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' )";
mysql_query($sql);
$file_location = '<a href="http://www.......com/upload/' . $thumbnail . '">' . $thumbnail . '</a>';