Skip to content

Instantly share code, notes, and snippets.

@alcalyn
Created February 22, 2015 13:22
Show Gist options
  • Save alcalyn/34bd55f4a028d29e8acc to your computer and use it in GitHub Desktop.
Save alcalyn/34bd55f4a028d29e8acc to your computer and use it in GitHub Desktop.
Get File data array from a multidimensionnal $_FILES post array
/**
* 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