如果我上传图像,然后编辑工作正常,但是如果我更新其他字段而不是图像,那它在cakephp2.4.5中将显示数组错误 [英] if i upload image then edit work fine but if i update other field and not image that time it display array to sting erro in cakephp2.4.5

查看:98
本文介绍了如果我上传图像,然后编辑工作正常,但是如果我更新其他字段而不是图像,那它在cakephp2.4.5中将显示数组错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

it will display error if i update other filed and not image.

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图像。谢谢

my cont code my view is below. so when i upload image it will work fine. but in edit if i dont upload image then it display array to sting error. means it not take ast image. thanks

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


推荐答案

写其他

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.



}

这篇关于如果我上传图像,然后编辑工作正常,但是如果我更新其他字段而不是图像,那它在cakephp2.4.5中将显示数组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆