Created
February 22, 2015 13:22
-
-
Save alcalyn/34bd55f4a028d29e8acc to your computer and use it in GitHub Desktop.
Get File data array from a multidimensionnal $_FILES post array
This file contains hidden or 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
/** | |
* Get a single file data from an array of files. | |
* Provide null for file keys ('name', 'tmp_name', ...) | |
* See example below | |
* | |
* @param array $files $_FILES | |
* @param array $path path to file data | |
* @return array file data | |
*/ | |
function getFileArray(array $files, array $path) { | |
$fileKeys = array( | |
'name', | |
'type', | |
'tmp_name', | |
'error', | |
'size', | |
); | |
$file = array(); | |
foreach ($fileKeys as $key) { | |
$value = $files; | |
foreach ($path as $p) { | |
$value = $value[(null === $p) ? $key : $p]; | |
} | |
$file[$key] = $value; | |
} | |
return $file; | |
} | |
// Example for a form with files named "files[0]" and "files[1]" : | |
getFileArray($_FILES, array('files', null, 0)); | |
getFileArray($_FILES, array('files', null, 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment