In PHP, when you submit a file input that has multiple files (as in, it is named <input ... name='foo[]' />), it organizes it oddly. The $_FILES array contains a single key, then within that key is an array with keys "name", "tmp_name", "size", "type", "error". Within each of those keys is an array with the data for each file.


Isn't it easier to have an array of files, with each one having its own data, instead of each data having its own files?

It seemed silly to me, so this code reorganizes it to make it loopable (so you can essentially loop through $_FILES properly).


<input type=file name='files[]'/>
$uploads = reorganizeMultiFileArray($_FILES, 'files');

Now, $uploads will contain your normal array of files, each containing name, temp name, filetype, etc.


function reorganizeMultiFileArray($files, $key) {
    $numfiles = count($files[$key]['error']);
    $newfiles = array_fill(0, $numfiles, array());
    foreach ($files[$key] as $k => $v) {
        for ($i = 0; $i < $numfiles; $i++) {
            $newfiles[$i][$k] = $v[$i];
    return $newfiles;

Next Post Previous Post