用cakephp3保存图片

save image with cakephp3

我想通过表格保存图片路径,但是图片的路径没有保存。

我试过 this 但我不知道把代码放在哪里

多媒体控制器:

public function add()
{
    $multimedia = $this->Multimedia->newEntity();
    if ($this->request->is('post')) {
        $multimedia = $this->Multimedia->patchEntity($multimedia, $this->request->data);

        $file = $_FILES['url'];
        $path = 'files/' .$_FILES['url']['name'];
        move_uploaded_file($this->data['url']['tmp_name'], $path);



        if ($this->Multimedia->save($multimedia)) {
            $this->Flash->success('The multimedia has been saved.');
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error('The multimedia could not be saved. Please, try again.');
        }
    }
    $categories = $this->Multimedia->Categories->find('list', ['limit' => 200]);
    $this->set(compact('multimedia', 'categories'));
    $this->set('_serialize', ['multimedia']);

}

add.ctp

 <?= $this->Form->create($multimedia, array('enctype' => 'multipart/form-data')); ?>
<fieldset>
    <legend><?= __('Add Multimedia') ?></legend>
    <?php
        echo $this->Form->input('title');
        echo $this->Form->input('description');
        echo $this->Form->input('mime_type');
        echo $this->Form->input('filename');
        echo $this->Form->input('url', array('type' => 'file'));

        echo $this->Form->input('category_id', ['options' => $categories]);
        echo $this->Form->input('created_by');

    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

我是 cakephp 3 的新手,有点不知所措。请帮忙。

更新:

已经实现保存图片:

 $file = $_FILES['url'];
        $path = "webroot\files\" .$_FILES['url']['name'];
        print_r($file);
        move_uploaded_file($_FILES['url']['tmp_name'], $path);

        if ($this->Multimedia->save($multimedia)) {
            $this->Flash->success('The multimedia has been saved.');
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error('The multimedia could not be saved. Please, try again.');
        }

但是现在"url"没有保存在数据库中

希望对你有所帮助,这里的脚本有一些改进,比如对数组使用短减速:

The Controller code:

//don't forget to import the Folder class
use Cake\Filesystem\Folder;

public function add()
{
    $multimedia = $this->Multimedia->newEntity();
    if ($this->request->is('post')) {
            $multimedia = $this->Multimedia->patchEntity($multimedia, $this->request->data);

            //upload script start here
            $mm_dir = new Folder(WWW_ROOT . 'upload_dir', true, 0755);
            $target_path = $mm_dir->pwd() . DS . $this->request->data('url.name');
            move_uploaded_file($this->request->data('url.tmp_name'), $target_path);

            //save the file name in the field 'url'
            $multimedia->url= $this->request->data('url.name');
            //the script ends here

            if ($this->Multimedia->save($multimedia)) {
                    // ............
            } else {
                    // .........
            }
    }
    // ..........
}

add.ctp // improvement

<?= $this->Form->create($multimedia, ['type' => 'file']); ?>
echo $this->Form->input('url', ['type' => 'file']);