2012-10-25 37 views
0

我使用cakephp FormHelper生成html表單代碼。cakephp如何在上傳多個文件時分組文件數據

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 
       ) 
     ) 
) 

非常感謝您的幫助。

回答

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; 
}