Converting Array in PHP ($_FILES)
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 th开发者_运维技巧at I want ?
Thanks
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
}
}
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]);
}
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]);
}
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>';
精彩评论