How to set image file upload size limit in PHP

Create image file upload script in php with image file maximum upload size limit define.

Image file upload size limit represents here with the total size on an image that a user want to upload on their server. This type of restriction gives the facility to website owner that its user cannot upload high quality size images. As a result website hosting uses low space and bandwidth to again serve that images on website. So in this tutorial we are creating a simple php image uploading file script with upload size limit condition and if the selected image is more then the defined maximum size limit, then it will cancel the upload and display message that please select low size images to continue upload. So here is the complete step by step tutorial for How to set image file upload size limit in PHP.

android-project-download-code-button

How to set image file upload size limit in PHP.

Code for upload-file-limit.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Set image file upload size limit in PHP</title>
</head>

<body>
<table>
<form action="" method="post" enctype="multipart/form-data">
 <tr>
 <td> <input type="file" name="uploadFile" id="uploadFile"></td></tr>
 <tr><td> <input type="submit" value="Click Here To Upload" name="submit"></td></tr>
</form>
</table>
</body>
</html>

<?php

//Adding isset button function on submit button.
if(isset($_POST["submit"])) {
 
 if (!empty($_FILES["uploadFile"]["name"])) {
 
 $file_save_folder = "upload_folder/";
 $file_save = $file_save_folder . basename($_FILES["uploadFile"]["name"]);
 $UploadCode = 1;
 $Image_Type = pathinfo($file_save,PATHINFO_EXTENSION);
 
 $File_Check = getimagesize($_FILES["uploadFile"]["tmp_name"]);
 
 if($File_Check !== false) {
 
 $UploadCode = 1;
 
 } else {
 echo "File is not an image.";
 $UploadCode = 0;
 }
 
 // Checking file size
if ($_FILES["uploadFile"]["size"] > 50000) {
 echo "Please select low size Image to upload.";
 $UploadCode = 0;
}

// Allow only jpg, jpeg, png,gif formats.

if($Image_Type != "jpg" && $Image_Type != "jpeg" && $Image_Type != "gif" && $Image_Type != "png" 
 ) {
 echo "Sorry, You can only upload JPG,JPEG,PNG,GIF Files";
 $UploadCode = 0;
}


if ($UploadCode == 0) {
 echo "Try Again";

} 
else {
 if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $file_save)) {
 echo "Your File has been successfully uploaded.";
 } 
}

}
else {
 
 echo "Please select file first to upload ";
 
 }
}
?>

Screenshot:

set image file upload size limit in PHP

Click here to download set image file upload size limit in PHP project files with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *