Jump to content

Multiple File Upload


RobertP

Recommended Posts

Everyone that has tried to upload multiple files via the 'multiple' attribute in their input, have noticed that you get a 'stupid' array on the server side.

 

Results when uploading a single file.

$_FILES['my_file'] = array(
    'tmp_name'=>'\tmp\204294.png',
    'name'=>'my-files-original-name.png',
    'size'=>2048,
    'type'=>'image/png',
    'error'=>0
);

 

 

Results when uploading multiple files.

$_FILES['my_files'] = array(
    'tmp_name'=>array(
        0=>'\tmp\204294.png',
        1=>'\tmp\573912.png',
        2=>'\tmp\019283.png',
    ),
    'name'=>array
        0=>'my-files-original-name-1.png',
        1=>'my-files-original-name-2.png',
        2=>'my-files-original-name-3.png',
    ),
    'size'=>array(
        0=>2048,
        1=>2022,
        2=>2334,
    ),
    'type'=>array(
        0=>'image/png',
        1=>'image/png',
        2=>'image/png',
    ),
    'error'=>array(
        0=>0,
        1=>0,
        2=>0,
    )
);

 

 

What i would expect from multiple files.

$_FILES['my_file'] = array(
    0=>array(
        'tmp_name'=>'\tmp\204294.png',
        'name'=>'my-files-original-name-1.png',
        'size'=>2048,
        'type'=>'image/png',
        'error'=>0
    ),
    1=>array(
        'tmp_name'=>'\tmp\533322.png',
        'name'=>'my-files-original-name-2.png',
        'size'=>2533,
        'type'=>'image/png',
        'error'=>0
    ),
    2=>array(
        'tmp_name'=>'\tmp\34662.png',
        'name'=>'my-files-original-name-3.png',
        'size'=>2332,
        'type'=>'image/png',
        'error'=>0
    )
);

 

 

Now the whole reason for this post is to help re-order the 'stupid' array that we get into what i think is a much better format.

This is my current function, but i would like something abit more cleaner, and have support for more fields.

    public static function separateMultipleFiles($rawFiles){
        $files = array();
        for($i=0;$i<count($rawFiles['tmp_name']);$i++){
            $files[] = array(
                'tmp_name'=>$rawFiles['tmp_name'][$i],
                'name'=>$rawFiles['name'][$i],
                'size'=>$rawFiles['size'][$i],
                'type'=>$rawFiles['type'][$i],
                'error'=>$rawFiles['error'][$i],
            );
        }
        return $files;
    }

Link to comment
https://forums.phpfreaks.com/topic/261143-multiple-file-upload/
Share on other sites

I came up with the following:

function organizeFileArray($array)
{
    $organizedArr = array();
    
    $tmpFunc = function($value, $key, $element) use (&$organizedArr)
    {
        if (! isset($organizedArr[$key])) {
            $organizedArr[$key] = array();
        }
        $organizedArr[$key][$element] = $value;
    };
    
    foreach (array_keys($array) as $key) {
        $element = $array[$key];
        array_walk($element, $tmpFunc, $key);
    }
    
    return $organizedArr;
}

It'll produce the same result as your function, but is a lot more dynamic.

The downfall is that, after a bit of benchmarking, your function is around 400% faster than mine.

So there is indeed room for optimization. I hope it helps, gl.

 

Edit:

I just noticed that the function array_walk is completely unnecessary in this scenario.

I modified the function to reflect this:

function organizeFileArray($array)
{
    $organizedArr = array();
    
    foreach (array_keys($array) as $key) {
        $element = $array[$key];
        $currElement = $key;
        foreach($element as $key => $value) {
            if (! isset($organizedArr[$key])) {
                $organizedArr[$key] = array();
            }
            $organizedArr[$key][$currElement] = $value;
        }
    }
    
    return $organizedArr;
}

Your function is only 80% faster than this - much less appalling than 400% faster.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.