cakephp上传多个文件时如何分组文件数据

时间:2012-10-25 15:33:04

标签: cakephp-2.2

我使用cakephp FormHelper生成html表单代码。

echo $this->Form->create('newGallery.', array('type'=>'file'));
    echo $this->Form->input('Photos',array(
            'type'  =>  'file',
            'label' =>  'Photos (jpg,png,gif)',
            'name'  =>  'upload[]',
            'required',
            'multiple'
        ));
echo $this->Form->end('Create');

但结果是

Array
(
    [upload] => Array
        (
            [name] => Array
                (
                    [0] => Pic1.jpg
                    [1] => Pic2.jpg
                )
            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )
            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\phpB073.tmp
                    [1] => C:\wamp\tmp\phpB084.tmp
                )
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )
            [size] => Array
                (
                    [0] => 216302
                    [1] => 107102
                )
        )
)

我需要对同一文件的数据进行分组,以便我可以使用foreach循环来处理。这是我需要的结果

Array
(
    [upload] => Array
        (
            [0] => Array
                (
                    [name] => Pic1.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\wamp\tmp\phpB073.tmp
                    [error] => 0
                    [size] => 216302
                )
            [1] => Array
                (
                    [name] => Pic2.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\wamp\tmp\phpB084.tmp
                    [error] => 0
                    [size] => 107102
                )
        )
)

谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我认为这可以帮助你:)

/* create new empty array */
$result=array();

/* create new index */
$i=0;

/* foreach in your $upload or other array */
foreach($upload['upload'] as $key => $val){
    foreach($val as $sub_val){
        $result[$i][$key]=$sub_val;
        $i++;
    }
    $i=0;
}
相关问题