添加图片上传工作正常…但在编辑的情况下,没有上传发生。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(); ?>
由于要上传文件,因此必须告诉Form处理程序创建适当的HTML。在编辑视图中,应该以:
开头 <?php echo $this->Form->create('Movie', array('type' => 'file'));?>
我想你在添加视图中做了这个,但是在这里忘记了。
提示:通过将表单生成代码放在单独的文件中,为添加和编辑视图使用相同的代码,然后包含()该代码片段