0

I am not able to upload a file from one folder to another in CakePHP. Here is my code.

First I loaded the plugin and added this code in ProductsTable.php

$this->addBehavior('Xety/Cake3Upload.Upload', [
    'fields' => [
        'productimg_file' => [
            'path' => 'uploads/avatar/:id/:md5'
        ]
    ]
]);

then I added in my add.cpt

<?php
    echo $this->Form->input('productcode');
    echo $this->Form->input('productname');
    echo $this->Form->input('productprice');
    echo $this->Form->input('quantity');
    echo $this->Form->input('productdesc');
    echo $this->Form->input('productimg_file',['type' => 'file']);
?>

Still the file doesn't get moved to the webroot dir.

enter code here public function add() {

    $product = $this->Products->newEntity();


    if ($this->request->is('post')) {

    $product = $this->Products->patchEntity($product, $this->request->data);

        if ($this->Products->save($product)) {
           $this->Flash->success(__('The product has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
           $this->Flash->error(__('The product could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('product'));
    $this->set('_serialize', ['product']);
}
4

3 回答 3

0

我已经通过那个插件成功上传了。一开始,我是你的错误,但我修复了它。我的步骤在这里...

内:CakePHP 3.2 Red Velvet

  1. 下载源复制UploadBehavior.php/src/Model/Behavior

  2. 复制"xety/cake3-upload": "1.*"(用双引号“”)粘贴到“require-dev”中的/config/bootstrap.php:{...},

前任:

"require-dev": {
        "psy/psysh": "@stable",
        "cakephp/debug_kit": "~3.2",
        "cakephp/bake": "~1.1",
        "xety/cake3-upload": "1.*"
    },
  1. 打开终端(window cmd)类型:

    作曲家更新

注意:如果你没有作曲家,谷歌并安装它。

=> 我知道 1 号和 2 号是一样的,但我肯定做到了。

  1. 配置为插件手册。下面的配置在Model/Table/ model_nameTable.php不是Model/Entity/model.php

前任:

$this->addBehavior('Xety/Cake3Upload.Upload', [
        'fields' => [
            'avatar' => [
                'path' => '/img/avatars/:id/:md5'
                    ]
                ]
            ]
        );
  1. 设置更新 URL

    '/img/folder_name/:id/:md5'

前任:

'/img/avatars/:id/:md5'
  1. 确保视图中的 FORM 定义为:

    $this->Form->create($user, ['enctype' => 'multipart/form-data'])

并且输入文件是正确的名称:

$this->Form->file('fieldname_file');

前任:

$this->Form->file('avatar_file');

不需要从控制器调用任何东西,当你提交表单时,它会自动上传。

祝你好运 !

于 2016-06-21T17:47:40.137 回答
0

你的观点似乎不完整。没有$this->Form->create()

插件说明提到您必须将表单设置为接受文件:

$this->Form->create($foo, ['type'=>'file']);
// .. or ..
$this->Form->create($foo, ['enctype' => 'multipart/form-data']);

上传的内容默认存储在/app/webroot/img/. 确保此文件夹可由 Apache 或 Nginx 写入。

于 2015-12-12T20:43:18.983 回答
0

_file 是一个默认后缀,您只能在 .ctp 文件中使用它。不在模型中。改变你在模型中的行为。希望这会有所帮助。

$this->addBehavior('Xety/Cake3Upload.Upload', [
    'fields' => [
        'productimg' => [
            'path' => 'uploads/avatar/:id/:md5'
        ]
    ]
]);
于 2017-07-05T14:44:50.043 回答