我的网站有许多用户报告下载一个大文件(80 MB)的问题。我正在使用头信息来强制下载。如果需要,我可以提供其他的php设置。我正在使用CakePHP框架,但这段代码是普通的php。我使用的是CentOS Linux上的apache服务器和来自media temple的专用虚拟服务器,并且php版本为5.2。您是否看到以下代码存在任何问题:
set_time_limit(1500);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($file_path) . "\"");
header("Content-Length: ".$content_length);
header("Content-Transfer-Encoding: binary");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Pragma: public');
header('Expires: 0');
//Change this part
$handle = fopen($file_path, 'rb');
while (!feof($handle))
{
echo fread($handle, 4096);
ob_flush();
flush();
}
fclose($handle);
exit;
基本上,报告的问题是下载开始后中途停止。我认为这可能是时间限制的问题,所以我添加了set_time_limit代码。之前我使用的是php readfile函数,但也没有顺利工作。
readfile
的东西完全可以避免将其放入内存中(除非启用了输出缓冲-但即使您按块读取它,也会有同样的问题)。 - El Yobo