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
)
注意它是如何都是一樣的信息?