Here are the inputs I want to loop through
Main photo: <input type="file" name="image[]" />
Side photo 1: <input type="file" name="image[]" />
Side photo 2: <input type="file" name="image[]" />
Side photo 3: <input type="file" name="image[]" />
A couple weird things happened, when I uploaded nothing I use the count($_FILES['image'])
, I echoed that function, and it returns a value of 5. There should be no elements in that array. Why is there one extra input when I only have 4 files to begin with?
Now with the actually looping itself, I try using the foreach loop, but it doesn't work.
foreach($_FILES['image'] as $files){echo $files['name']; }
Nothing came up, what I wanted to ultimately do is to loop through all images, make sure they are correct format, size, and rename each of them. But this simple foreach() loop shows that somehow I can't even loop through the $_FILES array and the count() confused me even more when it say that there are 5 elements in the array when I didn't even upload anything.
I have struggled with this dilemma for almost a week! Nothing I found on the net could help me. I knew sort of what to do, but could not figure out how to loop through the $_FILES array properly - until now when I read the edited post of the accepted answer.
I made some changes though, in the script as posted, as it did not work properly for me. I wanted to be able to determine if a file was selected at all, so I changed the line "if( !empty( $_FILES[ 'image' ][ 'error' ][ $index ] ) )" to "if( !empty( $_FILES[ 'image' ][ 'size' ][ $index ] ) )" and then instead of "return false;", I put the size into a variable instead: "$Size = $_FILES[ 'upload' ][ 'size' ][ $index ];"
This way I could check if the $Size variable was larger than zero. If it was, then a file had been selected and I could continue with counting the number of files and do the actual upload. I did not use any of the "unnecessary" script after "return false;", in the accepted answer. Hope this helps someone.
: P /MacD
I came up with a solution that works for $_FILES arrays of arbitrary depth. As a quick explanation, what you need an algorithm that does this:
Here's some code that actually works.
Just call sane_files_array on $_FILES and you should be good to go, regardless of how deep the $_FILES array is. This really should be part of the language itself, because the formatting of the $_FILES array is absolutely ridiculous.
Short function to rebuild $_FILES['files'] to some more expected structure.
Maybe:
If you have to loop through specific file fields.
Your example form should work fine. It's just that you are expecting the structure of the
$_FILES
superglobal to be different than it actually is, when using an array structure for the field names.The structure of this multidimensional array is as followed then:
Therefor
count( $_FILES[ "fieldname" ] )
will yield5
.But counting deeper dimensions will also not produce the result you may expect. Counting the fields with
count( $_FILES[ "fieldname" ][ "tmp_name" ] )
for instance, will always result in the number of file fields, not in the number of files that have actually been uploaded. You'd still have to loop through the elements to determine whether anything has been uploaded for a particular file field.EDIT
So, to loop through the fields you would do something like the following:
For more information see the docs.
PHP's choice of how to handle $_FILES wastes a lot of developer time. Based on @Lendrick's answer, here is a similar OO approach.