Skip to content

Instantly share code, notes, and snippets.

@pedrosancao
Last active October 27, 2015 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrosancao/9100827 to your computer and use it in GitHub Desktop.
Save pedrosancao/9100827 to your computer and use it in GitHub Desktop.
Parses the PHP's $_FILES array when multiple file input is used, it will return an array as each file was from a different input. It also works when multiple and single inputs are mixed
<?php
/**
* Parses the PHP's $_FILES array when multiple file input is used, it will
* return an array as each file was from a different input. It also works
* when multiple and single inputs are mixed
*
* @author Pedro Sanção <pedro at sancao do co>
* @license MIT Licence
*/
function parse_files() {
$files = array();
foreach ($_FILES as $fieldName => $file) {
reset($file);
$key = key($file);
if (is_array($file[$key])) {
$propeties = array_keys($file);
$fieldNamekeys = array_keys($file[$key]);
foreach ($fieldNamekeys as $namekey) {
$fileKey = "{$fieldName}_{$namekey}";
$files[$fileKey] = array();
foreach ($propeties as $property) {
$files[$fileKey][$property] = $file[$property][$namekey];
}
}
} else {
$files[$fieldName] = $file;
}
}
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment