簡體   English   中英

CakePHP 3.x多個文件上傳不起作用

[英]CakePHP 3.x multiple file upload not working

使用該行時:

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

該表格甚至不會提交。 就像頁面被重新加載一樣。 此代碼用於工作。 另外,如果我使用ad_photos。 而不是ad_photos []我收到此錯誤消息:

Cannot get an empty property

如果我只使用ad_photos而不帶點或方括號,則此request-> data中只會顯示一個文件。 我試圖調試和var_dump數據,如下所示,但它甚至都沒有使用方括號將其放入代碼的那部分,僅用點引發了錯誤。

控制器方法:

public function add()
{
    $listing = $this->HavesAndWants->newEntity();
    $error = '';
    if ($this->request->is('post')) {
        debug($this->request->data()); exit;
        echo "<pre>"; var_dump($this->request->data()); echo "</pre>"; exit;
        $listing = $this->HavesAndWants->patchEntity($listing, $this->request->data);
        $listing->user_id = $this->Auth->user('id');
        $listing->ad_photos = '';
        if ( $error ) {
            $this->Flash->error(__('The listing could not be saved. Please, try again.'));
        } else {
            if ($this->HavesAndWants->save($listing)) {
                $this->Flash->success(__('The listing has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The listing could not be saved. Please, try again.'));
            }
        }
    }
    $this->set(compact('error', 'listing'));
    $this->set('_serialize', ['listing']);
}

查看表格:

<?= $this->Form->create($listing, ['type' => 'file']) ?>
        <h1><?= __('Add Listing') ?></h1>
        <fieldset>
            <legend><?= __('Contact Info') ?></legend>
            <?= $this->Form->input('contact_name'); ?>
            <?= $this->Form->input('contact_email'); ?>
            <?= $this->Form->input('contact_phone'); ?>
            <?= $this->Form->input('contact_street_address'); ?>
            <?= $this->Form->input('contact_city'); ?>
            <?= $this->Form->input('contact_state'); ?>
            <?= $this->Form->input('contact_zip'); ?>
        </fieldset>
        <fieldset>
            <legend><?= __('Listing Info') ?></legend>
            <?= $this->Form->input('ad_title'); ?>
            <?= $this->Form->input('ad_street_address'); ?>
            <?= $this->Form->input('ad_city'); ?>
            <?= $this->Form->input('ad_state'); ?>
            <?= $this->Form->input('ad_zip'); ?>
            <?= $this->Form->input('ad_additional_info', ['label' => 'Ad Description']); ?>
            <?= $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => true, 'label' => 'Add Some Photos']); ?>
        </fieldset>
        <?= $this->Form->button(__('Submit')) ?>
        <?= $this->Form->end() ?>

我有點愚蠢,但是安全組件使我感到沮喪。 我所要做的就是解鎖ad_photos字段,並且能夠上傳多個文件。 雖然,我不確定為什么我一開始就被蒙蔽。 如果有人有任何想法,請在下面發布。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM