I have a file upload form set up with the HTML5 multiple attribute.
However, the form still only uploads a single file. Do i need to create some sort of a looping function in the php or is there another way of doing this?
Here's my code...
form:
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<input type="file" multiple="multiple" name="file[]" id="file" />
<input name="submit" type="submit" value="Submit" />
</form>
php:
<?php
if(isset($_POST['submit'])) {
foreach($_FILES['newsImage'] as $file){
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
}
?>
I believe this code could serve the purpose. It loops through the
$_FILES
array and creates an array withkey => value
pair of all the attributes for for each file.I believe your field should be
<input type="file" multiple="multiple" name="files[]" />
And then in PHP: