Make no Error appear, if nothing is chosen

This code (below) works successfully.
If the wrong type of file is chosen to upload
“Error - Invalid File Name” appears.

Because it’s optional to upload a file, if no file is chosen to upload,
“Error - Invalid File Name” still appears.

What can I add so that the Error only appears if a wrong type of file is chosen, and not appear if nothing is chosen?

$allowedExts = array("gif", "jpeg", "jpg", "pdf", "doc", "docx", "txt", "rtf", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = strtolower( end($temp) );
if (!in_array($extension,$allowedExts))
echo ("Error - Invalid File Name");
$length = 20;
$randomString = (time());
$thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $thumbnail);
$sql = "INSERT INTO videos ( filename ) VALUES( '$thumbnail' )";
$file_location = '<a href="' . $thumbnail . '">' . $thumbnail .     '</a>';

Hi Chris,

$_FILES will be empty if the user hasn’t uploaded a file, so just put in a conditional check for that.

Thanks for your reply.
Could you, please, give me an example of a “conditional check” that would work here?

if (!empty($_FILES)) {
    // process uploaded files

Thanks for your example>
I’m not sure what to add to that so that the rest of the Form simply proceeds if it’s empty.
Any additional guidance will be appreciated.

It goes around the part of your validation that is dealing with the file upload.

All of the code you already have remains in place except for the extra if statement around the one section you want to skip over when there is no file to validate.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.