Reputation: 47
I would like to create 2 file inputs and upload both of them to the server. I did it already with 1 file in that way:
<input type="file" name="file" id="file" accept="image/*" step="1">
And the php script something like that:
<?php
if ($_FILES["file"]["error"] > 0)
echo "Error: " . $_FILES["file"]["error"] . "<br>";
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
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"];
}
}
?>
But now when I want to have 2 inputs, I duplicate the html code:
<input type="file" name="file1" id="file1" accept="image/*" step="1">
<input type="file" name="file2" id="file2" accept="image/*" step="1">
And I have no idea what I need to change or duplicate in my php script.
Upvotes: 0
Views: 60
Reputation: 1176
With the html you have you have two $_FILES multidimensional arrays one referenced as
$_FILES['file1'][]
and the other as
$_FILES['file2'][]
.
doing a var_dump($_FILES) in your code to will help interrogate what information is being submitted.
You may have to duplicate this block, I've shown code based on your example set to file1 or use a foreach loop such as
foreach( $_FILES as $file ) {
//reference each value using $file as follows.
$file['name'];
$file['size'];
}
Upvotes: 0
Reputation: 599
$_FILES["file"]
references the uploaded file from
<input ... name="file">
so the first level of $_FILES
are the name
s of the HTML file inputs. You can either duplicate your code and change every $_FILES['file']
to $_FILES['file1']
(and $_FILES['file2']
in the duplicated code), or you loop through the $_FILES
array - although this could be a security risk, as one could easily trick your script into storing more than two files.
Upvotes: 1