Yii2 Download File function

萝らか妹 提交于 2020-05-25 18:39:19

问题


I need to download file from folder /uploads, but I just get timeOut error when the action is called, Anyone can help me :(

   public function actionDownload() {
   $path = Yii::getAlias('@webroot') . '/uploads';

   $file = $path . '/1.pdf';

   if (file_exists($file)) {

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

  } 
}

回答1:


If a download takes too much time, I see 2 possibilities

  • You can increase the max execution time of your script. That's not the best solution as the script will still time out for too big files but that's the simplest solution (there may be performance considerations unrelated to your question). To do so :

ini_set('max_execution_time', 5*60); // 5 minutes

  • You can use the X-SendFile header of Apache (if this module in enabled in Apache only) to let Apache handle the sending of the file. More about this on Yii2 documentation http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html#sending-files. Beware of bugs in IE<=8.

if (file_exists($file)) { Yii::$app->response->xSendFile($file); }



来源:https://stackoverflow.com/questions/30724938/yii2-download-file-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!