0

如果我更新其他文件而不是图像,它将显示错误。

public function edit($id = null) {

$this->helpers = array('TinyMCE.TinyMCE');
$this->layout = 'adminpanel';
if (!$id) {
    throw new NotFoundException(__('Invalid post'));
}

$this->layout = 'adminpanel';
//save data
if ($this->request->is(array('post', 'put'))) {

    $this->Tour->id = $id;

    //Save image
    if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
    {

            $fileNameFull = $this->request->data['Tour']['varbigimg']['name'];

                 $uploadFolder = "upload";
                 //full path to upload folder
                $uploadPath = WWW_ROOT . $uploadFolder;
                $oldFile = $uploadPath.'/'.$fileNameFull; 

            move_uploaded_file(
              $this->request->data['Tour']['varbigimg']['tmp_name'],$oldFile
            );

            $newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull; 

            $image = new ImageResizeComponent();
            $quality = 100; // image resize for thumb
            $height = 40;
            $width = 60;
            $this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
            $this->request->data['Tour']['varbigimg'] = $fileNameFull;
        } 
    else{//Img not uploaded
 $this->request->data['Tour']['vartitle']=  $this->data['Tour']['vartitle'];   
 $this->request->data['Tour']['varsubtitle']=  $this->data['Tour']['varsubtitle']; 
 $this->request->data['Tour']['txtsortdesc']=  $this->data['Tour']['txtsortdesc']; 
$this->request->data['Tour']['txtdeasc']=  $this->data['Tour']['txtdeasc'];   
 $this->request->data['Tour']['vardeparts']=  $this->data['Tour']['vardeparts']; 
 $this->request->data['Tour']['decadultprice']=  $this->data['Tour']['decadultprice']; 
 $this->request->data['Tour']['decchildprice']=  $this->data['Tour']['decchildprice'];   
 $this->request->data['Tour']['varimgtitle']=  $this->data['Tour']['varimgtitle']; 
 $this->request->data['Tour']['enumstatus']=  $this->data['Tour']['enumstatus']; 
   $this->request->data['Tour']['id']=  $this->data['Tour']['id']; 
//In this way do for All Except Image.
}
  //     pr($this->$this->request->data);
    if ($this->Tour->save($this->request->data)) {              
    $this->Session->setFlash(__('Unable to add your schedule.'));

    //Save image
    $this->Session->setFlash(__('Your tour has been updated.'));
    return $this->redirect(array('controller'=>'admin','action' => 'tour'));    
    $this->Session->setFlash(__('Unable to update your Tour.'));
    }
}
$tour = $this->Tour->findByid($id);
if (!$tour) {
    throw new NotFoundException(__('Invalid post'));
}
if (!$this->request->data) {
    $this->request->data = $tour;
}

}

我的续代码我的观点如下。所以当我上传图片时它会正常工作。但是在编辑中,如果我不上传图像,那么它会显示数组以刺痛错误。意味着它不采用 ast 图像。谢谢

echo $this->Form->create('Tour',array('autocomplete' => 'off','enctype'=>'multipart/form-data'));
echo $this->Form->input('varbigimg',array('type' => 'file'));?>
4

1 回答 1

0

写其他为

if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
{
   .........
}else{

     //**TWO GOLDEN LINES OF YOUR LIFE**
     $tourForImg = $this->Tour->findByid($id);
     $this->request->data['Tour']['varbigimg'] = $tourForImg['Tour']['varbigimg'];
     //**TWO GOLDEN LINES OF YOUR LIFE**


     //AS Img not uploaded by user
     //Write All data EXPLICITELY that you want to save WITHOUT Image.
     $this->request->data['Tour']['Tourname']=  $this->data['Tour']['Tourname'];   
     $this->request->data['Tour']['YourFormField1']=  $this->data['Tour']['YourFormField1']; 
     $this->request->data['Tour']['YourFormField2']=  $this->data['Tour']['YourFormField2']; 
     //In this way do for All Except Image.



}
于 2014-10-30T10:05:55.583 回答