Create image file upload script in PHP to upload files on server online

How to design php scripts that can only accept JPG, JPEG, PNG & GIF files.

In this tutorial we are creating a simple php script with restriction that user can only upload four type of image format files and the all other types of files is restricted by php script. So here is the complete step by step tutorial for Create image file upload script in PHP to upload files on server online.

android-project-download-code-button

How to Create image file upload script in PHP to upload files on server online.

Code for file-upload.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Create image file upload script in PHP to upload files on server online</title>
</head>

<body>
<table>
<form action="" method="post" enctype="multipart/form-data">
 <tr><td> Select Image Here To Upload : </td>
 <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"] > 700000) {
 echo "Sorry, your file is too large.";
 $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 "Sorry,File not uploaded.";

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

}
else {
 
 echo "Please select file";
 
 }
}
?>

Screenshots:

Create image file upload script in PHP to upload files on server online

upload-dialog

successfully-upload-file-message

Click here to download Create image file upload script in PHP to upload files on server online project file with source code.

Leave a Reply

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