So I have this form in a for loop which submits to itself and gets images uploaded by users, the form code:
for ($x=1; $x<=10; $x++)
{
echo "<form method='post' enctype='multipart/form-data'>";
echo "
<label for='file'>Image:</label>
<input type='file' name='file'><br>
<input type='submit' name='submit' value='Upload Image'>
<br>";
include ('lib/upload.php');
}
the form will then call a script named "upload.php" which contains these codes:
<?php
if (isset($_POST['submit']))
{
$allowedext=array("gif", "png", "jpeg", "jpg");
$tmp=explode(".", $_FILES["file"]["name"]);
$ext=end($tmp);
if (
(($_FILES["file"]["type"]=="image/gif") ||
($_FILES["file"]["type"]=="image/jpeg") ||
($_FILES["file"]["type"]=="image/jpg") ||
($_FILES["file"]["type"]=="image/pjpeg") ||
($_FILES["file"]["type"]=="image/png") ||
($_FILES["file"]["type"]=="image/x-png")) &&
($_FILES["file"]["size"]<=3145728) && in_array($ext, $allowedext)
)
{
if ($_FILES["file"]["error"]>0)
{
echo "<br>Error: ".$_FILES["file"]["error"]."<br>";
}
else
{
if (file_exists("upload/".$_FILES["file"]["name"]))
{
echo "<br><p style='color: #ff0000;'>".$_FILES["file"]["name"]." already exists.</p><br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES["file"]["name"]);
$x="upload/".$_FILES["file"]["name"];
echo "<br><img src='$x' width='320px' height='240px'><br>";
}
}
}
else if ($_FILES["file"]["size"]>3145728)
{
echo "<br><p style='color: #ff0000;'>File too large</p><br>";
}
else
{
echo "<br><p style='color:#ff0000;'>Invalid file</p><br>";
}
}
?>
So my problem is, everytime I upload an image, the error "Invalid File" comes up even though I'm pretty sure I uploaded a valid file. This works if I loop the form once, like this:
for ($x=1; $x<=1; $x++)
{
\\code
}
any ideas what the problem could be?