简体   繁体   中英

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. instead of ad_photos[] I get this error message:

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. 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.

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. Although, I am unsure why I was being blackholed in the first place. If anyone has any ideas please post them below. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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