dousi0144 2016-02-04 07:07
浏览 18
已采纳

cakephp 3表单,文件类型不保存数据库中的图像数据

I am trying to upload image using cakephp 3. I tried it in cake 2.x, it worked fine there but not in cake 3.0. My image is uploaded but it is not getting saved in DB.

view

<div class="col-lg-8">
<div class="articles form large-9 medium-8 columns content">
    <?= $this->Form->create($article,['type' => 'file']) ?>
    <fieldset>
        <legend><?= __('Add Article') ?></legend>
        <?php
        echo $this->Form->input('title', [ "class" => "form-control"]);

        echo $this->Form->input('body', [ "class" => "form-control"]);

        echo $this->Form->file('image',[ "class" => "form-control"]);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit'), ["class" => "btn btn-primary"]) ?>
    <?= $this->Form->end() ?>
</div>

Controller

public function add() {
    $article = $this->Articles->newEntity();
    if ($this->request->is('post')) {
        $filepath = getcwd() . '/uploads/' . $this->request->data['image']['name'];
        $filename = $this->request->data['image']['name'];
        $article = $this->Articles->patchEntity($article, $this->request->data);
        if ($this->Articles->save($article)) {                
            move_uploaded_file($this->request->data['image']['tmp_name'], $filepath);
            $this->Flash->success(__('The article has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The article could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('article'));
    $this->set('_serialize', ['article']);
}

Model

namespace App\Model\Table;

use App\Model\Entity\Article;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class ArticlesTable extends Table {
public function initialize(array $config) { parent::initialize($config);

    $this->table('articles');
    $this->displayField('title');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->hasMany('Comments', [
        'foreignKey' => 'article_id'

    ]);
}


public function validationDefault(Validator $validator)
{
    $validator
        ->add('id', 'valid', ['rule' => 'numeric'])
        ->allowEmpty('id', 'create');

    $validator
        ->allowEmpty('title');

    $validator
        ->allowEmpty('body');

    $validator
        ->add('image', [
                    'fileSize' => [
                            'rule' => [
                                'fileSize', '<', '5MB'
                            ],
                            'message' => 'Please upload file smaller than 5MB'
                        ],
                    'mimeType' => [
                        'rule' => [
                            'mimeType', ['image/jpeg','image/png','image/jpg']
                        ],
                        'message' => 'Please upload only png images'
                    ]
                ]
        )
        ->requirePresence('image', 'create')
        ->notEmpty('image');
    return $validator;
}

}

  • 写回答

1条回答 默认 最新

  • dtcaw02086 2016-02-06 08:26
    关注

    I have found a solution myself but I am not sure whether it is the right way. But by doing this my problem has been solved.

    public function add() {
        $article = $this->Articles->newEntity();
        if ($this->request->is('post')) {
            $imageName = $this->request->data['image']['name'];
            $filepath = getcwd() . '/uploads/' . $imageName;
            $article = $this->Articles->patchEntity($article, $this->request->data);
            $article->image =  $imageName ;
    
            if ($this->Articles->save($article)) {                
                move_uploaded_file($this->request->data['image']['tmp_name'], $filepath);
                chmod($filepath, 0777);
                $this->Flash->success(__('The article has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The article could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('article'));
        $this->set('_serialize', ['article']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图