How to upload files in web folder in yii2 advanced template?

后端 未结 1 1817
眼角桃花
眼角桃花 2021-01-28 21:50

I try to upload files in backend and each time i uploaded a file it was successfully uploaded and successfully saved in the DB but it wasn\'t save to the directory i specified s

1条回答
  •  清歌不尽
    2021-01-28 22:31

    $path = '/uploads/';
    

    That means you are uploading files to /uploads, the top level folder in the system.

    Run cd /uploads in your terminal and check, most likely files were uploaded there.

    To fix that, you need to specify correct full path. The recommended way to do it is using aliases.

    In case of advanced application, you already have @backend alias, so you can use it:

    $this->img->saveAs(\Yii::getAlias("@backend/web/uploads/{$randomString}.{$this->img->extension}");
    

    Or create your own alias in common/config/bootstrap.php:

    Yii::setAlias('uploads', dirname(dirname(__DIR__)) . '/backend/web/uploads');
    

    And then use it like this:

    Yii::getAlias('@uploads');
    

    Don't forget to include use Yii if you are not in root namespace or use \Yii;

    If you want your images to be accessible on frontend, you can create symlink between frontend and backend images folders.

    0 讨论(0)
提交回复
热议问题