I need to upload multiple images via form. I thought that I will do it with no problem, but I have one.
When I try to do foreach and get image by image it is not acting like I hoped it will.
HTML
<form method="post" action="" enctype="multipart/form-data" id="frmImgUpload">
<input name="fileImage[]" type="file" multiple="true" />
<br />
<input name="btnSubmit" type="submit" value="Upload" />
</form>
PHP
<?php
if ($_POST)
{
echo "<pre>";
foreach ($_FILES['fileImage'] as $file)
{
print_r($file);
die(); // I want it to print first image content and then die to test this out...
//imgUpload($file) - I already have working function that uploads one image
}
}
What I expected from it to print out first image, instead it prints names of all the images.
Example
Array
(
[0] => 002.jpg
[1] => 003.jpg
[2] => 004.jpg
[3] => 005.jpg
)
What I want it to output
Array
(
[name] => 002.jpg
[type] => image/jpeg
[tmp_name] => php68A5.tmp
[error] => 0
[size] => 359227
)
So how can I select image by image in the loop so I can upload them all?
Okey I found solution and this is how I did it, probably not the best way but it works.
foreach ($_FILES['fileImage']['name'] as $f)
{
$file['name'] = $_FILES['fileImage']['name'][$i];
$file['type'] = $_FILES['fileImage']['type'][$i];
$file['tmp_name'] = $_FILES['fileImage']['tmp_name'][$i];
$file['error'] = $_FILES['fileImage']['error'][$i];
$file['size'] = $_FILES['fileImage']['size'][$i];
imgUpload($file);
$i++;
}
See Question&Answers more detail:os