如果我不想在cakephp 3.5中更新新图像,如何更新旧图像

时间:2018-06-07 07:14:14

标签: cakephp-3.0

正在使用cakephp 3.x并面临有关上传图片的一些问题,

如果我没有上传任何图片,它应该拍下较旧的图片,但是没有发生请检查,下面是我的代码

 public function edit($id = null)
{
    $this->viewBuilder()->layout('home-layout');
    $detail = $this->Details->get($id, [
        'contain' => []
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {
   if(!empty($this->request->data['img_name']['name'])){
                {
    $this->request->data['img_name'] = $filename;
    }   
    $detail = $this->Details->patchEntity($detail, $this->request- 
   >getData());
              }
   if(!empty($this->request->data['img_name']['name'])){
        $filename = $this->request->data['img_name']['name'];
        $uploadpath ='img/';
        $uploadfile = $uploadpath. $filename;
        if(move_uploaded_file($this->request->data['img_name'] 
          ['tmp_name'],$uploadfile))
        {
         $this->request->data['img_name'] = $filename;
          }   
    $detail = $this->Details->patchEntity($detail, $this->request- 
   >getData());
        }   
        else{
      $detail = $this->Details->patchEntity($detail, $this->request- 
          >getData());
        }


  if ($this->Details->save($detail)) {
         $this->Flash->success(__('The detail has been saved.'));

        return $this->redirect(['action' => 'index']);
          $this->Flash->error(__('The detail could not be saved. Please, try 
     again.'));


   }
  $this->set(compact('detail'));

  }

0 个答案:

没有答案