【发布时间】:2019-08-22 13:52:34
【问题描述】:
我正在使用下面的代码下载文件。它使用 flush() 和 ob_flush() 函数。我在此阅读了 brenns10 评论 link
它说使用 flush() 和 ob_flush() 会导致数据进入内存直到显示出来,因此它不好 对于资源有限的服务器。我在共享服务器上。
我需要对此进行解释。我应该在下面的代码中将 flush() 和 ob_flush() 作为它的,还是应该删除它。谢谢
$download_file = '10gb_file.zip';
$chunk = 1024; // 1024 kb/s
if (file_exists($download_file) && is_file($download_file)) {
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($download_file));
header('Content-Disposition: filename=' . $download_file);
$file = fopen($download_file, 'r');
while(!feof($file)) {
print fread($file, round($chunk * 1024));
ob_flush();
flush();
}
fclose($file);
}
【问题讨论】:
-
您是否在此代码之前的任何地方使用
ob_start?如果没有,则不需要ob_flush。flush也取决于 phps 环境。 -
问题应该是,这个庞大的网站的文件是否应该首先通过PHP脚本下载,答案是否定的。
标签: php