Luqman
Luqman

Reputation: 249

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

Hey guys I am trying to set a download button where a user can download a file, which is uploaded using kartik's File Input Widget(single upload).

The codes resides inside the CRUD generated view/controllers/models.

This is the view button code,

        <?= Html::a('Download Uploaded File', ['download', 'id' => $model->form_id], [
    'class' => 'btn btn-danger',
    'data' => [
        'confirm' => 'Are you sure you want to download this 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 which is inside the 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 is the error log, 'Trying to get 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 the Download action(controller)

Upvotes: 0

Views: 1555

Answers (3)

Aswin Mohanan
Aswin Mohanan

Reputation: 151

You need to make sure set_time_limit, is enabled on the server to get the download option working, as it will be disabled on servers by default.

Upvotes: 0

Anamika Shrivastava
Anamika Shrivastava

Reputation: 713

Issue is in your download function.You are not saving extension in your model.There are many ways to get extension of file .

public function actionDownload($id) {
    $model = $this->findModel($id);

   $path = Yii::getAlias('@webroot') . '/uploads';
   $fileextension=end(explode('.',$model->fm_upload));
   $file = $path.'/borang/'.$model->form_id.'.'.$fileextension;

   if (file_exists($file)) {

   Yii::$app->response->sendFile($file);

  }
}

Upvotes: 1

Insane Skull
Insane Skull

Reputation: 9358

Try this 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()

Upvotes: 1

Related Questions