I am trying to run an application in Yii2 I tried to upload a user profile picture and it works greatly but the problem starts when I try to update the name file. It shows me an error like Call to a member function saveAs() on a non-object. I even tried to update without any changes… even that time also show me the same error… but error goes if I change the pic. this is my controller
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// get the instance of uploaded file
$imageName = $model->user_username;
$model->file = UploadedFile::getInstance($model,'user_avatar');
$model->file->saveAs( '/uploads/'.$imageName.'.'.$model->file->extension );
//save the path in DB..
$model->user_avatar = 'uploads/'.$imageName.'.'.$model->file->extension;
$model->save();
return $this->redirect(['view', 'id' => $model->user_id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}