In web application development, we often get requirement like uploading files, images on your website from client machine, sometimes we allow uploading any type of file, sometime only some specific types.
In this tutorial you will learn how to upload files in php, that can be any type of files like excel, images etc, also check if the file already exists.
Php script can be used for uploading files in web application, PHP code allows you to upload single and multiple files to a temporary location then move to a targeted destination directory, at the same the file reference can be saved into mysql database from php code.
First make sure that PHP INI file is configured to allow file upload.
In "php.ini" file, you need to set file_uploads to On
file_uploads = On
First we create a simple html form with a file control and submit button, so user can select file from local machine and upload to web server.
Here are some rules for html form:<form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="fileUpload1" id="fileUpload1"/> <input type="submit" value="Upload" /> </form>
Let's create a file called "file-upload.php" which will contain following php script to handle the form posting requirement.
<?php $target_dir = "uploadedFiles/"; $target_file = $target_dir . basename($_FILES["fileUpload1"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileUpload1"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } ?>
You can set limit to file size for user, so they cannot upload large file size, you need to check the posted file size before processing the request. in below example we have set the file should be with in 700KB.
// Check the posted file size if ($_FILES["fileToUpload"]["size"] > 700000) { echo "Sorry, your file should be 700KB or less."; $uploadOk = 0; }
You can restrict user uploading same file again and again, if same file already exists in that folder you can display appropriate error message.
$uploadOk = 1; // Check if any file already exists with same name if (file_exists($target_file)) { echo "Sorry, same file already exists."; $uploadOk = 0; }
For example you want user to upload only jpg or png file , so if the file type are different than expected file type , then don’t upload , display some error message to user
$target_file = $target_dir . basename($_FILES["fileUpload1"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; }
Here is the actual script for uploading file on server, but before you execute this part of script, you need to check all above validation for successful file upload in your web application
if (move_uploaded_file($_FILES["fileUpload1"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileUpload1"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; }
Below is the complete working php script for file uploading with validation on web server? You can use this in your web application directly, only make sure you have folder called “uploadedFiles” in your root, if you want to change the folder name then you must change the directory name in script also
<?php $target_dir = "uploadedFiles/"; $target_file = $target_dir . basename($_FILES["fileUpload1"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileUpload1"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size is too big if ($_FILES["fileUpload1"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // check if file formats are right if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") { echo "Invalid format, only JPG, PNG files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // finally, if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileUpload1"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileUpload1"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?>
Have fun with file uploading in php ready script!