0

I have a very basic code to upload form:

echo "<form action='upload.php' method='post' enctype='multipart/form-data'>";
echo "Select upload:   ";
echo "<input type='file' name='fileToUpload' id='fileToUpload'>";
echo "<input type='submit' value='Upload file' name='submit'>";
echo "</form>";

Ok, now I want to check the input if the user has selected a file or not, but before sending the input filename via POST to another PHP file (upload.php)

I know that checking if no file has been selected can be done something like this:

if(!isset($_FILES['fileToUpload']) || $_FILES['fileToUpload']['error'] == UPLOAD_ERR_NO_FILE) {
    echo "Error no file selected"; 
} else {
    echo "ok";
}

I know that somehow I need the check the input here in this file:

echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">';

But how can mix these together ? I want to pass the filename to another php file, when a file is selected.

Thank you.

1
  • you can use JS to check that file has been selected or not before submitting the data. Commented Aug 18, 2015 at 10:12

2 Answers 2

1

If you want to check this with javascript you can use following

var filename = document.getElementById("DealImage").value;

if (filename != ''){
   alert('files selected');
}else{
   alert('empty files input');
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use jQuery to check file is selected or not on form submit.

if ( $('#fileToUpload').val() == '' || $('#fileToUpload').val() == null) {
    alert("Please select file");
}

1 Comment

Thank you for the help user27715!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.