I have an upload form where I want to allow users to upload images and videos but when I submit the form, it tosses the error message I have set into the else statement. What did I do wrong?
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["upload"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "video/avi")
|| ($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 100000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["upload"]["error"] > 0)
{
echo "Return Code: " . $_FILES["upload"]["error"] . "<br>";
}
else
{
$fileName = $temp[0].".".$temp[1];
$temp[0] = rand(0, 3000); //Set to random number
$fileName;
if (file_exists("content/" . $_FILES["file"]["name"]))
{
echo $_FILES["upload"]["name"] . " already exists. ";
}
else
{
$newfilename = rand(1,99999).end(explode(".",$_FILES["upload"]["name"]));
move_uploaded_file($_FILES["upload"]["tmp_name"], "content/" . $newfilename);
echo "Stored in: " . "content/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file or connection failure.";
}
?>
"Invalid file or connection failure." is the error message I get.
ifstatement. Can you post your<form>code ?$_FILES["file"]["name"];to$_FILES["upload"]["name"];mismatched. Since there are others called$_FILES["upload"]["name"])or make the first parameters all the same.ifstatement you only have to do anin_arraycomparison rather than that crazy long list.