Created
September 14, 2010 14:28
-
-
Save lslucas/579124 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Upload multiple files with php | |
<?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]); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment