使用Qimage编辑图片上传无法使用cakephp

时间:2015-07-19 09:32:09

标签: php cakephp

添加图片上传工作正常...但在编辑的情况下,没有上传发生。 DB保留旧图像数据。我正在使用Qimage进行文件复制 图像上传控制器

public function edit($id=NULL){
            if (!$id) {
                throw new NotFoundException(__('Invalid Movie'));
            }
            $movie = $this->Movie->findById($id);
            if (!$movie) {
                throw new NotFoundException(__('Invalid Movie'));
            }
            if ($this->request->is(array('post', 'put'))) {
            $this->Movie->id = $id;
            if(!empty($this->request->data['Movie']['image']['name'])){
                    $data['file']= $this->request->data['Movie']['image'];
                    $data['path']= WWW_ROOT . 'img/';
                    unset($this->request->data['Movie']['image']);
                    $this->request->data['Movie']['image']=$this->Qimage->copy($data);
                    $this->Qimage->resize(array(
                        'width'=>200, 'height'=>300,
                        'file'=>WWW_ROOT . 'img/'.$this->request->data['Movie']['image'],
                        'output'=>WWW_ROOT . 'img/'
                        ));
                }
                else{
                    unset($this->request->data['Movie']['image']);
                }

                if ($this->Movie->saveAll($this->request->data['Movie'])) {
                    $this->Session->setFlash(__('Movie has been updated.'));
                    return $this->redirect(array('controller' => 'movies','action' => 'movielist'));
                }else{
                    $this->Session->setFlash(__('Unable to update movie.'));
                }
            }
            if (!$this->request->data) {
                $this->request->data = $movie;
            }

}

请帮忙。 编辑功能视图 -

<?php echo $this->Form->create('Movie');?>
<fieldset>
        <legend><?php echo __('Update Movie'); ?></legend>
        <?php
        echo $this->Form->input('id', array('type' => 'hidden')); 
        echo $this->Form->input('name');
        echo $this->Form->input('about');
        echo $this->Form->file('Movie.image');
        echo $this->Form->input('budget');
        echo $this->Form->submit('Update Movie', array('class' => 'form-submit',  'title' => 'Click here to update the movie')); 
?>
</fieldset>
<?php echo $this->Form->end(); ?>

1 个答案:

答案 0 :(得分:0)

由于您要上传文件,因此必须告诉Form处理程序创建正确的HTML。在编辑视图中,您应该从:

开始

<?php echo $this->Form->create('Movie', array('type' => 'file'));?>

我认为您在添加视图中执行了此操作,但在此处忘记了它。

提示:通过将表单生成代码放在单独的文件中,然后包含()该代码片段,对“添加”和“编辑”视图使用相同的代码。