Checking PHP file download - is it possible before sending input via POST?

advertisements

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.


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');
}