PHP File Uploading With Validation
While uploading the file with the PHP we can perform the validation on the file, Like must be the image, file size must be the less than or maximum or limit the file size, file is exist or not.
Check this page if you don’t know how to upload the file in the PHP: PHP File Uploading with vary basic explanation
PHP check uploading file is image or not
- We can get the extension of the file in the PHP using the pathinfo() method.
- pathinfo() method will return the extension of the file without the .(dot).
$extension = array("jpeg","jpg","png","gif"); // Add the extension to the above array to allow the users to // upload the file from above extension. $ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION); // By using the above method we can get the extension of the file // uploaded by the user. if(in_array($ext, $extension)){ move_uploaded_file($_FILES["file"]["tmp_name"], "UploadFolder/".$_FILES["file"]["name"]); echo "File is successfully uploaded!!!"; } else{ echo "Please, Select the file from the following extension: jpeg, jpg, png, gif"; }
PHP limit the size of the uploading file
- $_FILES[] gloabl array have the “size” index which will return the size of the uploading file.
- $_FILES[“control_name”][“size”] index will return the size of the file in the bytes. So be careful before using it.
$bytes = 1024; $allowedKB = 100; $totalBytes = $allowedKB * $bytes; if($_FILES["file"]["size"] <= $totalBytes){ move_uploaded_file($_FILES["file"]["tmp_name"], "UploadFolder/".$_FILES["file"]["name"]); echo "File uploaded successfully!!!"; } else{ echo "File size must be less than 100KB!!!"; }
PHP check the uploading file is already exist or not
- We can also check the file uploading by the user is already exist or not.
- We can use the file_exists() method of the PHP to check the file is exist or not.
$target_file = "UploadFolder/".$_FILES["file"]["name"]; if(file_exists($target_file)){ echo "File is already exist!!!"; } else{ move_uploaded_file($_FILES["file"]["tmp_name"],$target_file); echo "File uploaded successfully!!!"; }
PHP file uploading with validation full code
Let’s create the strip which upload the file only the type of the image with the size limit of the 100KB, If the uploading file is the already on the server than skip the file.
$errors = array(); $extension = array("jpeg","jpg","png","gif"); $ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION); if(!in_array($ext, $extension)){ array_push($errors, "File type is invalid, Please select image only."); } $bytes = 1024; $allowedKB = 100; $totalBytes = $allowedKB * $bytes; if($_FILES["file"]["size"] > $totalBytes){ array_push($errors, "File size must be less than 100KB!!!"); } if(file_exists("UploadFolder/".$_FILES["file"]["name"])) { array_push($errors, "File is already exist!!!"); } $count = count($errors); if($count == 0){ move_uploaded_file($_FILES["file"]["tmp_name"],"UploadFolder/".$_FILES["file"]["name"]); echo "File uploaded successfully!!!"; } else{ foreach($errors as $error){ echo $error."; } }
Click here to download the source code of the PHP File Uploading With Validation
Check this page if you don’t know how to upload the file in the PHP: PHP File Uploading with vary basic explanation