蛋糕PHP 3.x多文件上传不起作用



当使用下列行时:

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

表格甚至不会提交。就像页面被重新加载一样。此代码用于工作。另外,如果我使用ad_photos。而不是ad_photos[],我收到此错误消息:

Cannot get an empty property

如果我只使用没有点或括号的ad_photos,则此请求>数据中仅显示一个文件。我尝试调试和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字段,然后我能够上传多个文件。虽然,我不确定为什么我一开始就被黑洞了。如果有人有任何想法,请在下面发布。:)

最新更新