Last active
October 27, 2015 12:29
-
-
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
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
<?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