2017-02-14 97 views
0

我已經使用以下方式上傳圖像並將其保存在服務器中。現在我想寫一個控制器來更新上傳的圖像。有任何想法嗎?更新yii2中的上傳文件

控制器

public function actionCreate() 
    { 
    $model = new DoctorTbl(); 

    if ($model->load(Yii::$app->request->post())) { 

     $image = UploadedFile::getInstance($model, 'd_img_path'); 

     $model->d_img_path= $model->d_nic.'.'.$image->extension; 

     if ($model->save()) { 
      $image->saveAs('uploads/'.$model->d_img_path); 

      return $this->redirect(['view', 'id' => $model->d_id]); 


     } 


    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 

回答

2
public function Update($id) 
{ 
$model = $this->findModel($id); 
$oldImage = $model->d_img_path; 
if ($model->load(Yii::$app->request->post())) 
{ 
    $image = UploadedFile::getInstance($model, 'd_img_path'); 
    if(isset($image)){ 
     $model->d_img_path= $model->d_nic.'.'.$image->extension; 
    } else { 
     $model->d_img_path = $oldImage; 
    } 
    if($model->save()) 
    { 
     if(isset($image)){ 
      $image->saveAs('uploads/'.$model->d_img_path); 
     } 
    } 
    return $this->redirect('view'); 
}  
} 
+0

由於它的工作原理。 –