Before writing script to upload files to server, you have to check ensure that php file upload is set to 'On' in php.ini. Open C:\xampp\php\php.ini file locate the line that says upload_file = On and also change the php upload max setting, example: php_max_upload = 100m. Now save the file, restart the server and start writing php script to upload your file.
Step 1: Below is the html code to implement our practice of file upload. You can write it manually or copy and paste.
Step 2: Php command to target the upload folder, php comman to get file name, php command to check upload status, and php command to validate file extension. Below is our sample code:
Step 3: We will use php global object called $_POST to get the image object. And check if the file is image or not, if it's not image the upload status will be set to zero. Below is our sample code:
File is an image - " . $check["mime"] . ".
"; $status = 1; } else { echo "File is not an image.
"; $status = 0; } }Step 4: We will use php function object called file_exists() to check if the file already exist. If the file already exist, our upload status is set to zero.
Sorry, file already exists.
"; $status = 0; }Step 5: We will validate the size of file that should be allows to be uploaded in our server. If the file is too large php will reject the upload. Below is our upload sample files size validation.
Sorry, your file is too large.
"; $status = 0; }Step 6: We will check file extension, we don't allow every file to be uploaded to our server. If upload file with extension we don't accept, php should reject the upload Below is file extension validation.
Sorry, only JPG, JPEG, PNG & GIF files are allowed.
"; $status = 0; }Step 7: If everything goes well try upload our file. But if status was set to zero, return error message or continue to upload the file.
Sorry, your file was not uploaded.
"; } else { if (move_uploaded_file($_FILES["fileUpload"]["tmp_name"], $image)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileUpload"]["name"])). " has been uploaded.
"; } else { echo "Sorry, there was an error uploading your file.
"; } }Finally, below is complete code to upload our file, you can do copy and paste.
File is not an image.
"; $status = 0; } if (empty($image)) { echo "Sorry, you did not select file to upload.
"; $status = 0; } if (file_exists($image)) { echo "Sorry, file already exists.
"; $status = 0; } if ($_FILES["fileUpload"]["size"] > 500000) { echo "Sorry, your file is too large.
"; $status = 0; } if($filetype != "jpg" && $filetype != "png" && $filetype != "jpeg" && $filetype != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.
"; $status = 0; } if ($status == 0) { echo "Sorry, your file was not uploaded.
"; } else { if (move_uploaded_file($_FILES["fileUpload"]["tmp_name"], $image)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileUpload"]["name"])). " has been uploaded.
"; } else { echo "Sorry, there was an error uploading your file.
"; } } } ?>If the above code spot error, we can get in touch, you can contact us for further code review. Thanks for reading our articles.
Comments