note 116146 deleted from reserved.variables.files by salathe
| From: | salathe@php.net | Date: | Mon, 17 Nov 2014 15:57:59 +0000 |
| Subject: | note 116146 deleted from reserved.variables.files by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200938@lists.php.net to get a copy of this message | ||
Note Submitter: info at hypejunction dot com
----
This can be used to normalize $_FILES superglobal in cases when forms may contain multiple file
inputs with array and non-array names:
<?php
function normalize_files_superglobal() {
$result = array();
if (!is_array($_FILES) || !count($_FILES)) {
return $result;
}
foreach ($_FILES as $input_name => $uploads) {
$params = array_keys($uploads);
foreach ($params as $key) {
if (is_array($uploads[$key])) {
foreach ($uploads[$key] as $index => $value) {
$result[$input_name][$index][$key] = $value;
}
} else {
$result[$input_name][0][$key] = $uploads[$key];
}
}
}
return $result;
}
?>
BEFORE:
array (size=2)
'upload1' =>
array (size=2)
'name' => string 'upload1.txt' (length=11)
'type' => string 'text/plain' (length=10)
'upload2' =>
array (size=2)
'name' =>
array (size=2)
0 => string 'upload2-1.txt' (length=13)
1 => string 'upload2-2.jpeg' (length=14)
'type' =>
array (size=2)
0 => string 'text/plain' (length=10)
1 => string 'image/jpeg' (length=10)
AFTER:
array (size=2)
'upload1' =>
array (size=1)
0 =>
array (size=2)
'name' => string 'upload1.txt' (length=11)
'type' => string 'text/plain' (length=10)
'upload2' =>
array (size=2)
0 =>
array (size=2)
'name' => string 'upload2-1.txt' (length=13)
'type' => string 'text/plain' (length=10)
1 =>
array (size=2)
'name' => string 'upload2-2.jpeg' (length=14)
'type' => string 'image/jpeg' (length=10)