2

I would like to handle the multiple file uploads $_FILES in an array like this

foreach ($_FILES as $file) {
 // do stuff...
 // $file['name'] and as such
}

However the array seems something like this

Array ( 
 [name] => Array ( 
 [0] => 2010年10月04日_205047.jpg 
 [1] => 
 [2] => 
 ) 
 [type] => Array (
 [0] => image/jpeg 
 [1] => 
 [2] => 
 ) 
 [tmp_name] => Array ( 
 [0] => E:\localhost\tmp\php118.tmp 
 [1] => 
 [2] => 
 ) 
 [error] => Array ( 
 [0] => 0 
 [1] => 4 
 [2] => 4 
 ) 
 [size] => Array ( 
 [0] => 92127 
 [1] => 0 
 [2] => 0 
 ) 
) 

How should I make it into the array of the format that I want ?

Thanks

asked Nov 17, 2010 at 10:19
1
  • Here is an example of how to do it that I wrote to similar question. Commented May 8, 2013 at 6:31

4 Answers 4

7

This is kludgey, but

$_MYFILES = array();
foreach(array_keys($_FILES['name']) as $i) { // loop over 0,1,2,3 etc...
 foreach(array_keys($_FILES) as $j) { // loop over 'name', 'size', 'error', etc...
 $_MYFILES[$i][$j] = $_FILES[$j][$i]; // "swap" keys and copy over original array values
 }
}
answered Nov 17, 2010 at 10:30
Sign up to request clarification or add additional context in comments.

3 Comments

I think creating keys manually will take less resources compared to using array_keys since its always the same ?
True, but this is PHP... no guarantees that a key won't be renamed, or new ones added, etc... later on. Not likely, but not impossible. The inner array_keys call could be done once and cached in a var before the first foreach loop, though.
I completely agree to it .. Thanks Marc
0

why don't you want to declare new array var and to fill it in desirable format? eg

$myarr = array();
foreach ($_FILES as $file) {
 $myarr[] = array($file['name'][0], $file['type'][0], $file['tmp_name'][0], $file['size'][0]);
}
answered Nov 17, 2010 at 10:24

2 Comments

should be 'tmp_name', not 'temp_name' (isn't PHP's consistent naming convention a wonderful thing? :p)
@heximal In all seriousness I think you've misunderstood the problem.
0

this worked for me

$files = array();
for($i =0;$_FILES['name'][$i] != NULL;$i++){
 $files[] = array($_FILES['name'][$i], $_FILES['type'][$i], $_FILES['tmp_name'][$i], $_FILES['size'][$i]);
 }
answered Apr 14, 2017 at 13:10

Comments

0
foreach ($_FILES['photo']['name'] as $key => $value){
 $photo[$key]['name'] = $value;
}
foreach ($_FILES['photo']['type'] as $key => $value) {
 $photo[$key]['type'] = $value;
}
foreach ($_FILES['photo']['tmp_name'] as $key => $value) {
 $photo[$key]['tmp_name'] = $value;
}
foreach ($_FILES['photo']['error'] as $key => $value) {
 $photo[$key]['error'] = $value;
}
foreach ($_FILES['photo']['size'] as $key => $value) {
 $photo[$key]['size'] = $value;
}
if ($photo[0]['error'] == 4)
 $photo = [];
echo '<pre>';
print_r($photo);
echo '</pre>';
answered May 25, 2020 at 19:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.