file upload - Call to a member function saveAs() on a non-object in yii2 -


i trying run application in yii2 tried upload user profile picture , works problem starts when try update name file. shows me error call member function saveas() on non-object. tried update without changes.. time show me same error... error goes if change pic. controller

public function actionupdate($id) {     $model = $this->findmodel($id);      if ($model->load(yii::$app->request->post()) && $model->save()) {          // instance of uploaded file         $imagename = $model->user_username;         $model->file = uploadedfile::getinstance($model,'user_avatar');          $model->file->saveas( '/uploads/'.$imagename.'.'.$model->file->extension );          //save 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,         ]);     } } 

the model rules

  public function rules()             {                 return [                  [['user_email', 'user_password'], 'required'],                     // [['user_password'], 'required'],                     [['user_username'], 'string', 'max' => 45],                     [['user_first_name'], 'required'],                     [['user_app_user_id'], 'required',                     [['user_type','user_app_name','user_status'], 'string'],                     [['user_app_user_id'], 'integer'],                     [['user_rememberme'], 'boolean'],                     [['user_first_name', 'user_last_name'], 'string', 'max' => 100],                     [['user_email'], 'string', 'max' => 150],                     [['user_password'], 'string', 'min'=>6],                     [['user_password'], 'string', 'max' => 40,                     [['user_avatar'],'file'],                     //[['user_avatar'], 'string', 'max' => 500],                     [['user_verification_code','user_auth_key'], 'string'],                     [['user_email'], 'email'],                     [['user_email'], 'filter', 'filter' => 'trim'],                      ];             } 

your code requires file upload. should add condition here:

if ($model->file = uploadedfile::getinstance($model,'user_avatar')) {     $model->file->saveas( '/uploads/'.$imagename.'.'.$model->file->extension );     //save path in db..     $model->user_avatar = 'uploads/'.$imagename.'.'.$model->file->extension;     $model->save(); } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -