note 96983 added to features.file-upload.multiple
| From: | CoreyBallou at osu1 dot php dot net | Date: | Thu, 25 Mar 2010 21:10:39 +0000 |
| Subject: | note 96983 added to features.file-upload.multiple | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167421@lists.php.net to get a copy of this message | ||
Here is a function to fix the indices of a multi-dimensional for easier parsing when dealing with
file uploads. It takes a single $_FILES field array as a parameter and separates each individual
uploaded file by numeric key. This allows for iterating like:
<?php
fixFilesArray($_FILES['array_of_files']);
foreach ($_FILES['array_of_files'] as $position => $file) {
// should output array with indices name, type, tmp_name, error, size
var_dump($file);
}
?>
Here's the code:
<?php
/**
* Fixes the odd indexing of multiple file uploads from the format:
*
* $_FILES['field']['key']['index']
*
* To the more standard and appropriate:
*
* $_FILES['field']['index']['key']
*
* @param array $files
* @author Corey Ballou
* @link http://www.jqueryin.com
*/
function fixFilesArray(&$files)
{
$names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1,
'error' => 1, 'size' => 1);
foreach ($files as $key => $part) {
// only deal with valid keys and multiple files
$key = (string) $key;
if (isset($names[$key]) && is_array($part)) {
foreach ($part as $position => $value) {
$files[$position][$key] = $value;
}
// remove old key reference
unset($files[$key]);
}
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 96.10.168.94
----
Manual Page -- http://www.php.net/manual/en/features.file-upload.multiple.php
Edit -- https://master.php.net/note/edit/96983
Del: integrated -- https://master.php.net/note/delete/96983/integrated
Del: useless -- https://master.php.net/note/delete/96983/useless
Del: bad code -- https://master.php.net/note/delete/96983/bad+code
Del: spam -- https://master.php.net/note/delete/96983/spam
Del: non-english -- https://master.php.net/note/delete/96983/non-english
Del: in docs -- https://master.php.net/note/delete/96983/in+docs
Del: other reasons-- https://master.php.net/note/delete/96983
Reject -- https://master.php.net/note/reject/96983
Search -- https://master.php.net/manage/user-notes.php