Page 1 of 1
Processing multiple arrays from HTML form
Posted: Fri Jul 30, 2010 3:40 pm
by staggan
Hello
I am trying to post multiple files from a HTML form to a php page for processing but I always seem to end up with an array that is either 0, 5 or something else, but not the number of files I uploaded...
This is the post form...
Code: Select all
<form method="post" action="process.php" enctype="multipart/form-data">
<div>
<input type="file" name="upload[]" /><BR>
<input type="file" name="upload[]" /><BR>
<input type="file" name="upload[]" /><BR>
<input type="file" name="upload[]" /><BR>
<input type="submit" value="Upload Ad " />
</div>
</form>
</div>
</body>
</html>
And in the process page the first thing I do is check the number of files in the array like this...
Code: Select all
$uploads = count ($_FILES ['upload'] ['name']);
I then echo the $uploads to the screen
If I add two files in the form I get 14 as the $uploads...
What am I missing?
I don't seem able to get the number of files I posted from that page at all...
Any help would be appreciated, I am sure the answer is simple, but it is driving me crazy
Thanks
Re: Processing multiple arrays from HTML form
Posted: Fri Jul 30, 2010 3:47 pm
by jraede
I'm not sure how arrays work with the $_FILES global, but assuming it's the same as $_POST and $_GET, you're getting the count of the wrong variable. count($_FILES['upload']) should return the amount of files uploaded...I don't know where you got $_FILES['upload']['name']
EDIT:
Just to clarify, the $_FILES global holds information about each file in an array. So if your file uploads are in an array to begin with, you'd have to access the name property like $_FILES['upload'][0]['name']. I *think* that's how it works, but again I'm not sure if the $_FILES global behaves the same way with array form fields as $_POST and $_GET.
Re: Processing multiple arrays from HTML form
Posted: Fri Jul 30, 2010 4:09 pm
by John Cartwright
When in doubt, look at how the array is structured.
Code: Select all
echo '<pre>'. print_r($_FILES, true) .'</pre>';
Re: Processing multiple arrays from HTML form
Posted: Fri Jul 30, 2010 5:26 pm
by staggan
Thanks for that...
That code returns this...
Code: Select all
Array
(
[upload] => Array
(
[name] => Array
(
[0] => Image1.png
[1] => Image2.png
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
)
[type] => Array
(
[0] => image/x-png
[1] => image/x-png
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
)
[tmp_name] => Array
(
[0] => /var/tmp/phpFehcQh
[1] => /var/tmp/php6NzTqw
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 4
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
[9] => 4
[10] => 4
[11] => 4
[12] => 4
[13] => 4
)
[size] => Array
(
[0] => 18762
[1] => 18762
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
[13] => 0
)
)
)
So the array is valid...
But when I try and do a Foreach ($_FILES['upload'] as $file) { DO STUFF}
Its not working... I assumed this function would allow me to run the "DO STUFF" once for each element in the array... but it doesn't seem to...
I had to use count ($_FILES['upload']['name']) to give me the number of files uploaded as using just count ($_FILES['upload']) was returning the number of items for each element in the array...
Re: Processing multiple arrays from HTML form
Posted: Fri Jul 30, 2010 5:37 pm
by jraede
Look at the array it printed out. Each part of the typical $_FILE array is combined into its own array, so all the names are in an array, all the types are in an array, etc. So a foreach would go through the names, then the types, then the tmp names, etc. What I would do is run the foreach loop on $_FILE['upload']['name'], and use the array key to access the related values in the other arrays.
Re: Processing multiple arrays from HTML form
Posted: Fri Jul 30, 2010 5:56 pm
by AbraCadaver
The count will always be 14 because all file inputs are submitted even if they are empty:
Code: Select all
echo count(array_filter($_FILES ['upload']['name']));
Re: Processing multiple arrays from HTML form
Posted: Sat Jul 31, 2010 6:31 am
by staggan
So, I now have my file count working correctly, and at the moment I am making sure I upload all files rather than leave elements in the array empty...
Now I run a loop to go through the uploaded files like this:
$fileloop = count ($_FILES ['upload']['name']);
for ($arrayindex = 0; $arrayindex < $fileloop ; $arrayindex++)
{ DO STUFF}
This should be working, but what I want to do now is assign each of the image arrays within the upload array to another temporary array for processing...
and I am trying to do it like this...
$image = $_FILES['upload'] ['$arrayindex'];
But its not working... what am I missing?
Re: Processing multiple arrays from HTML form
Posted: Sat Jul 31, 2010 9:20 am
by jraede
You don't have to use quotes if you're defining an array index with a variable. Also, PHP doesn't parse strings wrapped with single-quotes (') for variables, so it's setting the index of, literally, $array_index, not the value that it represents.
Re: Processing multiple arrays from HTML form
Posted: Sat Jul 31, 2010 5:13 pm
by staggan
Thanks for the help, I finally got this part working.. was much more complicated than I expected... I have to use the foreach ($_FILES ['upload'] ['name'] as $loop) as a counter for the number of files uploaded, and then use another loop foreach (array_keys ($_FILES ['upload']) as $key) to give me all the key names.. and then I loop through each key and asign it to another clean array, and eventually end up with a single image array from my posting... I can then process this single image array...
The only thing I am having trouble with now is the empty elements posted in my form...
If I have:
File 1 <input type="file" name="upload[]" /><BR>
File 2 <input type="file" name="upload[]" /><BR>
File 3 <input type="file" name="upload[]" /><BR>
and I only add File1 and File3 .. how can I prevent the empty array for File2 being posted?
Thanks
Re: Processing multiple arrays from HTML form
Posted: Sat Jul 31, 2010 5:23 pm
by superdezign
Look at the $_FILES['upload']['error'] array. The
error codes let you know if the upload was successful or not. Error code 0 means that the upload was successful. Error code 4 means that no file was uploaded (in other words, no file was given). The PHP manual has a section on
handling uploads. It's worth a read.
Re: Processing multiple arrays from HTML form
Posted: Sun Aug 01, 2010 10:30 am
by AbraCadaver
Code: Select all
$files = array_filter($_FILES ['upload']['name']);
Then use the $files array.
Re: Processing multiple arrays from HTML form
Posted: Mon Aug 02, 2010 3:13 am
by staggan
This is my code, which now works, based on what has been suggested and based on my own reading...
//SETUP FILE UPLOAD INDEX
$index=0;
//LOOP FOR ALL UPLOADED FILES
foreach ($_FILES ['upload'] ['name'] as $loop)
{
//CLEARS SINGLE IMAGE ARRAY
unset ($image);
//IF NAME IS EMPTY SKIP ITERATION AND UPDATE INDEX
if ($_FILES ['upload'] ['name'] [$index] == '' )
{
$index = $index+1;
continue;
}
//NAME IS NOT EMPTY SO FILL IMAGE ARRAY WITH CURRENT FILE DATA
foreach (array_keys ($_FILES ['upload']) as $key)
{
echo $index;
$image [$key] = ($_FILES ['upload'] [$key] [$index]);
}
//DISPLAY CURRENT IMAGE ARRAY
echo '<pre>'. print_r($image) .'</pre>';
$index = $index+1;
}
What I am doing is building a single image from all the uploaded image arrays which I can then process...
I use the first loop to loop through all the names of the uploaded files...
I then use the second loop to populate my temporary image array....
But to remove any empty image arrays uploaded as part of my form I use the
if ($_FILES ['upload'] ['name'] [$index] == '' )
{
$index = $index+1;
continue;
}
This skips the current iteration of my main loop if the current file array key name is empty... which means this is a blank array
All works as expected...
Thank you all for your help
Re: Processing multiple arrays from HTML form
Posted: Mon Aug 02, 2010 4:56 am
by superdezign
An easier way to get the index is in the foreach loop construct.
Code: Select all
foreach ($array as $index => $value) {
....
}
Re: Processing multiple arrays from HTML form
Posted: Thu Aug 05, 2010 7:06 am
by staggan
Thank you all for your help, this script now does what I want....
But I do have one other question....
I currently use the image['name'] element to decide which image I am updating / uploading, which is fine, and it works, but two things happen...
If the user uploads a different image in one of the slots in my form it still processes provided the image['name'] element matches one of my named slots....
and secondly I cannot upload of a file without a name matching that of one of my predefined names... so I can't upload an image called test.png as it will not work, since the name is not one I recognise
So, this is my form:
<B>AdBoard01:</B><input type="file" name="upload[]" /><BR>
<B>image01:</B><input type="file" name="upload[]" /><BR>
<B>image02:</B><input type="file" name="upload[]" /><BR>
<B>image03:</B><input type="file" name="upload[]" /><BR>
<B>image04:</B><input type="file" name="upload[]" /><BR>
<B>image05:</B><input type="file" name="upload[]" /><BR>
<B>image06:</B><input type="file" name="upload[]" /><BR>
<B>image07:</B><input type="file" name="upload[]" /><BR>
How do I define a name for each of these images and still end up with the multiple arrays as before? Or do I need to seperate it out to upload single arrays?
Hope that makes sense
Re: Processing multiple arrays from HTML form
Posted: Thu Aug 05, 2010 10:17 am
by AbraCadaver
Sorry, that doesn't make sense to me.