2015-06-30 178 views
1

如何獲取每個文件的所有數據?多個文件/圖像上傳CakePHP 3

我不確定如何使用CakePhp 3語法上傳多個文件。我已經看過插件的帖子,但我想知道如何做到這一點,而無需爲簡單的任務使用插件。

這裏是我的形式:

<div class="havesAndWants form large-10 medium-9 columns"> 
    <?= $this->Form->create($havesAndWant, ['type' => 'file']) ?> 
    <fieldset> 
     <legend><?= __('Add Haves And Want') ?></legend> 
     <?php 
      echo $this->Form->input('contact_name'); 
      echo $this->Form->input('contact_email'); 
      echo $this->Form->input('contact_phone'); 
      echo $this->Form->input('contact_street_address'); 
      echo $this->Form->input('contact_city'); 
      echo $this->Form->input('contact_state'); 
      echo $this->Form->input('contact_zip'); 
      echo $this->Form->input('ad_street_address'); 
      echo $this->Form->input('ad_city'); 
      echo $this->Form->input('ad_state'); 
      echo $this->Form->input('ad_zip'); 
      echo $this->Form->input('ad_additional_info', ['label' => 'Ad Description']); 
      echo $this->Form->input('ad_photos', ['type' => 'file', 'multiple' => 'multiple', 'label' => 'Add Some Photos']); 
     ?> 
    </fieldset> 
    <?= $this->Form->button(__('Submit')) ?> 
    <?= $this->Form->end() ?> 
</div> 

我似乎無法抓住所有的數據從多個文件。它抓取第一個文件,併爲每個文件分發相同的信息,即使它們是不同的文件。

控制器:

$photos = $this->request->data['ad_photos']; 
foreach ($photos as $photo) { 
       $photo = [ 
        'name' => $this->request->data['ad_photos']['name'], 
        'type' => $this->request->data['ad_photos']['type'], 
        'tmp_name' => $this->request->data['ad_photos']['tmp_name'], 
        'error' => $this->request->data['ad_photos']['error'], 
        'size' => $this->request->data['ad_photos']['size'] 
       ]; 
       echo "<pre>"; print_r($photo); echo "</pre>"; 
      } 

輸出:

Array 
(
    [name] => DPC.jpg 
    [type] => image/jpeg 
    [tmp_name] => C:\wamp\tmp\dpze123.tmp 
    [error] => 0 
    [size] => 2288982 
) 
Array 
(
    [name] => DPC.jpg 
    [type] => image/jpeg 
    [tmp_name] => C:\wamp\tmp\dpze123.tmp 
    [error] => 0 
    [size] => 2288982 
) 
Array 
(
    [name] => DPC.jpg 
    [type] => image/jpeg 
    [tmp_name] => C:\wamp\tmp\dpze123.tmp 
    [error] => 0 
    [size] => 2288982 
) 
Array 
(
    [name] => DPC.jpg 
    [type] => image/jpeg 
    [tmp_name] => C:\wamp\tmp\dpze123.tmp 
    [error] => 0 
    [size] => 2288982 
) 
Array 
(
    [name] => DPC.jpg 
    [type] => image/jpeg 
    [tmp_name] => C:\wamp\tmp\dpze123.tmp 
    [error] => 0 
    [size] => 2288982 
) 

注意它是如何都是一樣的信息?

回答

5

由於在CakePHPs的傢伙支持

http://webchat.freenode.net/?channels=cakephp&uio=MT1mYWxzZSY5PXRydWUmMTE9MjQ2b8

我得到了問題的答案。我以爲我需要使用$ post數組來設置文件信息的變量。在CakePhp Docs中,它顯示了一個很好的例子,它不是必需的。我拿出的代碼塊:

$photo = [ 
        'name' => $this->request->data['ad_photos']['name'], 
        'type' => $this->request->data['ad_photos']['type'], 
        'tmp_name' => $this->request->data['ad_photos']['tmp_name'], 
        'error' => $this->request->data['ad_photos']['error'], 
        'size' => $this->request->data['ad_photos']['size'] 
       ]; 

和我的輸入名稱的文件上傳這樣增加了[]:

echo $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => 'true', 'label' => 'Add Some Photos']); 

當時我能夠抓住所有每個文件的信息。