Image 使用cakephp3保存图像

Image 使用cakephp3保存图像,image,cakephp-3.0,Image,Cakephp 3.0,我想通过表单保存图像路径,但未保存图像路径 我试过了,但我不知道该把代码放在哪里 多媒体控制器: public function add() { $multimedia = $this->Multimedia->newEntity(); if ($this->request->is('post')) { $multimedia = $this->Multimedia->patchEntity($multimedia, $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() ?>

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

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

控制器代码:

add.ctp//改进


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

我希望这对您有所帮助,这里的脚本有一些改进,比如对阵列使用短减速:

控制器代码:

add.ctp//改进


echo$this->Form->input('url',['type'=>'file']);
 $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.');
        }
//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 {
                    // .........
            }
    }
    // ..........
}
<?= $this->Form->create($multimedia, ['type' => 'file']); ?>
echo $this->Form->input('url', ['type' => 'file']);