cakephp 3通过Utils Behavior上传图片

时间:2015-07-29 06:23:04

标签: php cakephp cakephp-3.0

我正在使用这个插件http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/#configurations在cakephp 3中上传图片。这里图片名称正在保存数据库,但问题是图片没有在路径上传。这是我尝试过的模型中的代码

 $this->addBehavior('Utils.Uploadable', [
            'banner' => [
                'field' => 'id',
                'path' => '{ROOT}{DS}{WEBROOT}{DS}uploads{DS}',
                'fileName' => '{field}.{extension}'
            ],
 ]);

我在我的webroot文件夹中创建了一个文件夹,调用uploads.But没有任何文件保存在uploads文件夹中,但名称保存在数据库表中。

1 个答案:

答案 0 :(得分:1)

我也在挣扎很长时间。最后试着自己解决它。这个插件有一个错误。在Uploadable行为中查找aftersave函数。

原始代码:

public function afterSave($event, $entity, $options)
{
    $fields = $this->getFieldList();
    foreach ($fields as $field => $data) {
        if ($this->_ifUploaded($entity, $field)) {
            if ($this->_uploadFile($entity, $field)) {
                if (!key_exists($field, $this->_savedFields)) {
                    $this->_savedFields[$field] = true;
                    $event->subject()->save($this->_setUploadColumns($entity, $field));
                }
            }
        }
    }

    $this->_savedFields = null;
}

FIX之后:

public function afterSave($event, $entity, $options)
{
    $fields = $this->getFieldList();
    foreach ($fields as $field => $data) {
        //LINES ADDED - Start
        if(isset($_FILES[$field])){
            $entity->{$field} = $_FILES[$field];
        }
        //LINES ADDED - end
        if ($this->_ifUploaded($entity, $field)) {
            if ($this->_uploadFile($entity, $field)) {
                if (!key_exists($field, $this->_savedFields)) {
                    $this->_savedFields[$field] = true;
                    $event->subject()->save($this->_setUploadColumns($entity, $field));
                }
            }
        }
    }

    $this->_savedFields = null;
}

这应该可以解决您的问题。 :)