FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

Plugin not working on cake 3.8 #525

Closed ZakkProjects closed 4 years ago

ZakkProjects commented 4 years ago

Hi i´ve installed the plugin with composer a added $this->addPlugin('Josegonzalez/Upload'); to model\application.php.

but after i´ve send the form file is not saved and no data are i database.

gallery/add.ctp

<div class="gallery form large-9 medium-8 columns content">
    <?= $this->Form->create($gallery) ?>
    <fieldset>
        <legend><?= __('Add Gallery') ?></legend>
        <?php
            echo $this->Form->control('title');
            echo $this->Form->control('img_url');
            echo $this->Form->control('img_alt');
            echo $this->Form->input('photo', ['type' => 'file']);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

Gallery controller:

public function add()
    {
        $gallery = $this->Gallery->newEntity();
        if ($this->request->is('post')) {
            $gallery = $this->Gallery->patchEntity($gallery, $this->request->getData());
            if ($this->Gallery->save($gallery)) {
                $this->Flash->success(__('The gallery has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The gallery could not be saved. Please, try again.'));
        }
        $this->set(compact('gallery'));
    }

Gallery table

public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('gallery');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->addBehavior('Josegonzalez/Upload.Upload', [
            'photo',
        ]);
    }
public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', null, 'create');

        $validator
            ->scalar('title')
            ->maxLength('title', 50)
            ->requirePresence('title', 'create')
            ->notEmptyString('title');

        $validator
            ->scalar('img_url')
            ->maxLength('img_url', 50)
            ->requirePresence('img_url', 'create')
            ->notEmptyString('img_url');

        $validator
            ->scalar('img_alt')
            ->maxLength('img_alt', 50)
            ->requirePresence('img_alt', 'create')
            ->notEmptyString('img_alt');

        $validator
            ->scalar('photo')
            ->maxLength('photo', 255)
            ->requirePresence('photo', 'create')
            ->notEmptyString('photo');

        return $validator;
    }
ZakkProjects commented 4 years ago

ok now i´ve got it to write the filename to database, but the file is not saving to webroot

davidyell commented 4 years ago

Your form type is not file.

<?= $this->Form->create($gallery, ['type' = 'file') ?>

Check the examples https://github.com/FriendsOfCake/cakephp-upload/blob/master/docs/examples.rst