我使用以下内容上传图片并将其保存在服务器中。现在我想编写一个控制器来更新上传的图像。有什么想法吗?
控制器
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,
]);
}
}
答案 0 :(得分:4)
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');
}
}