用cakephp3保存图像

时间:2015-06-30 22:04:45

标签: image cakephp-3.0

我想通过表单保存图像路径,但不保存图像的路径。

我已尝试使用this,但我不知道在何处放置代码

MultimediaController:

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

add.ctp

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

我是cakephp 3的新手,有点迷失。请帮忙。

更新

已经实现了保存图像:

from eve import Eve

app = Eve()
app.run()

但现在&#34; url&#34;未保存在数据库中

1 个答案:

答案 0 :(得分:0)

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

  

控制器代码:

//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 //改进

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