Yii 2. Upload document outside public folder

前端 未结 1 1488
时光说笑
时光说笑 2021-01-21 10:30

I have a requirement to upload a document to server, since it is a personal one, they want it to be uploaded outside public folder. I know how to upload a file:

         


        
相关标签:
1条回答
  • 2021-01-21 11:04

    Yes Yii can send file output, but you still have to create your own action.

    Lets assume following code in siteController Specific to image output, You can use same way to output other files.

    public function actionImage($image_path) {
          Yii::$app->getResponse()->sendFile(Yii::getAlias('@image_uploads') . $image_path);
    }
    

    now image src will be something like

    <img src="/site/image?image_path=/posts/1.png" /> or equivalent to the real application url routes

    So basic function to send file output by Yii2 is

    Yii::$app->getResponse()->sendFile();

    Located Under

    \yii\web\Response.php

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