How to check upload file is selected or not in PHP

How to detect input upload file is selected or not and display message please select file first to continue upload.

There are lot’s of free php image file uploading scripts available on internet but if you are a beginner in php development and want to first learn about file selection so if user dose not select file first and click on upload button then without showing any error message it will simply display you a message that please select the file to upload. So using this condition user can get more response without error on your page. This can be possible through an simple if condition with !empty $_FILES .So in this tutorial we are simply creating a php file uploading script with condition to select file first How to check upload file is selected or not in PHP.

android-project-download-code-button

How to check upload file is selected or not in PHP.

Code for check-upload-file.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>How to check upload file is selected or not 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;
 }

// 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 first to upload ";
 
 }
}
?>

check upload file is selected or not in PHP

Click here to download check upload file is selected or not in PHP project with source code.

Leave a Reply

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