Yii2 下载文件功能
Yii2 Download File function
我需要从文件夹 /uploads 下载文件,但在调用操作时出现超时错误,
任何人都可以帮助我:(
public function actionDownload() {
$path = Yii::getAlias('@webroot') . '/uploads';
$file = $path . '/1.pdf';
if (file_exists($file)) {
Yii::$app->response->sendFile($file);
}
}
如果下载时间太长,我认为有 2 种可能
- 您可以增加脚本的最长执行时间。这不是最好的解决方案,因为脚本仍然会因文件太大而超时,但这是最简单的解决方案(可能存在与您的问题无关的性能考虑因素)。为此:
ini_set('max_execution_time', 5*60); // 5 minutes
- 您可以使用 Apache 的 X-SendFile header(如果仅在 Apache 中启用此模块)让 Apache 处理文件的发送。关于 Yii2 文档的更多信息 http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html#sending-files。当心 IE<=8.
中的错误
if (file_exists($file)) {
Yii::$app->response->xSendFile($file);
}
我需要从文件夹 /uploads 下载文件,但在调用操作时出现超时错误, 任何人都可以帮助我:(
public function actionDownload() {
$path = Yii::getAlias('@webroot') . '/uploads';
$file = $path . '/1.pdf';
if (file_exists($file)) {
Yii::$app->response->sendFile($file);
}
}
如果下载时间太长,我认为有 2 种可能
- 您可以增加脚本的最长执行时间。这不是最好的解决方案,因为脚本仍然会因文件太大而超时,但这是最简单的解决方案(可能存在与您的问题无关的性能考虑因素)。为此:
ini_set('max_execution_time', 5*60); // 5 minutes
- 您可以使用 Apache 的 X-SendFile header(如果仅在 Apache 中启用此模块)让 Apache 处理文件的发送。关于 Yii2 文档的更多信息 http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html#sending-files。当心 IE<=8. 中的错误
if (file_exists($file)) {
Yii::$app->response->xSendFile($file);
}