Skip to content

Instantly share code, notes, and snippets.

@krmgns
Last active August 29, 2015 14:19
Show Gist options
  • Save krmgns/f7a7402be712d51a88bb to your computer and use it in GitHub Desktop.
Save krmgns/f7a7402be712d51a88bb to your computer and use it in GitHub Desktop.
Normalize $_FILES returning assoc array.
<?php
function normalizePostFiles(array $postFiles) {
$files = [];
if (isset($postFiles['tmp_name']) && is_array($postFiles['tmp_name'])) {
foreach ($postFiles['tmp_name'] as $i => $file) {
if (isset(
$postFiles['name'][$i], $postFiles['type'][$i],
$postFiles['size'][$i], $postFiles['error'][$i],
$postFiles['tmp_name'][$i]
)) {
$files[$i] = [
'name' => $postFiles['name'][$i],
'type' => $postFiles['type'][$i],
'size' => $postFiles['size'][$i],
'error' => $postFiles['error'][$i],
'tmp_name' => $postFiles['tmp_name'][$i],
];
}
}
}
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment