php - Yii2 sendFile() - Trying to get property of non-object -


hey guys trying set download button user can download file, uploaded using kartik's file input widget(single upload).

the codes resides inside crud generated view/controllers/models.

this view button code,

        <?= html::a('download uploaded file', ['download', 'id' => $model->form_id], [     'class' => 'btn btn-danger',     'data' => [         'confirm' => 'are sure want download item?',         'method' => 'post',     ], ]) ?> 

controller function(download),

public function actiondownload($id) {     $model = $this->findmodel($id);     $path = yii::getalias('@web') . '/uploads';    $file = '/borang/'.$model->form_id.'.'.$model->file->extension;     if (file_exists($file)) {     yii::$app->response->sendfile($file);    } } 

controller function(upload inside create action)

public function actioncreate()     {             $model = new formmovement();          if ($model->load(yii::$app->request->post())) {              $model->file = uploadedfile::getinstance($model, 'file');              if (!empty($model->file) && $model->validate()) {                     $model->fm_upload = 'uploads/borang/'.$model->form_id.'.'.$model->file->extension;                     $model->save();                     $model->file->saveas('uploads/borang/'.$model->form_id.'.'.$model->file->extension);                     return $this->redirect(['view', 'id' => $model->form_id]);             }else{                 $model->save();                 return $this->redirect(['view', 'id' => $model->form_id]);             }          } else {             return $this->render('create', [                 'model' => $model,             ]);         }     } 

this error log, 'trying property of non-object' in c:\xampp\htdocs\adminsys\frontend\controllers\formmovementcontroller.php:181

which points to,

$file = '/borang/'.$model->form_id.'.'.$model->file->extension; 

inside download action(controller)

try way:

public function actiondownload($id) {     $model = $this->findmodel($id);      $path = yii::getalias('@web') . '/uploads';     $ext = substr(strrchr($model->file,'.'),1);     $file = $path.$model->file;     $download = '/borang/'.$model->form_id.'.'.$ext;     if(file_exists($file))         yii::$app->response->sendfile($download); } 

strrchr()


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -