Skip to content

Instantly share code, notes, and snippets.

@ozgurg
Last active February 9, 2022 08:04
Show Gist options
  • Save ozgurg/c366b7033869016791345c85b76be926 to your computer and use it in GitHub Desktop.
Save ozgurg/c366b7033869016791345c85b76be926 to your computer and use it in GitHub Desktop.
PHP $_FILES Normalization (Single & Multiple)
<?php
function normalizeFiles()
{
$files = [];
$multiple = false;
foreach($_FILES as $name => $fileArray) {
if(is_array($fileArray["name"])) {
$multiple = true;
foreach($fileArray as $attrib => $list) {
foreach($list as $index => $value) {
$files[$name][$index][$attrib] = $value;
}
}
} else {
$files[$name][] = $fileArray;
}
}
if(!$multiple && count($files) === 1) {
$files = array_map("current", $files);
}
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment