Yii2 Download File function

24,554

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

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

Share:
24,554
ronydavid
Author by

ronydavid

Updated on March 06, 2020

Comments

  • ronydavid
    ronydavid about 4 years

    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);
    
      } 
    }