简体   繁体   English

CakePHP 3.x多个文件上传不起作用

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

When using the line: 使用该行时:

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

The form won't even submit. 该表格甚至不会提交。 It is like the page gets reloaded. 就像页面被重新加载一样。 This code use to work. 此代码用于工作。 Also, if I use ad_photos. 另外,如果我使用ad_photos。 instead of ad_photos[] I get this error message: 而不是ad_photos []我收到此错误消息:

Cannot get an empty property

If I just use ad_photos with no dot or brackets only one file shows up in this request->data. 如果我只使用ad_photos而不带点或方括号,则此request-> data中只会显示一个文件。 I have tried to debug and var_dump the data as shown below, but it doesn't even make it to that part of the code using brackets and throws an error with just the dot. 我试图调试和var_dump数据,如下所示,但它甚至都没有使用方括号将其放入代码的那部分,仅用点引发了错误。

controller method: 控制器方法:

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']);
}

view form: 查看表格:

<?= $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() ?>

I feel kind of stupid, but the security component was blackholing me. 我有点愚蠢,但是安全组件使我感到沮丧。 All I had to do was unlock the ad_photos field and I was able to upload multiple files. 我所要做的就是解锁ad_photos字段,并且能够上传多个文件。 Although, I am unsure why I was being blackholed in the first place. 虽然,我不确定为什么我一开始就被蒙蔽。 If anyone has any ideas please post them below. 如果有人有任何想法,请在下面发布。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM