提供使用 php 下载的大型 Zip 文件

发布于 2024-11-24 23:36:22 字数 1034 浏览 0 评论 0原文

我使用的下载代码如下..

ob_start();
ini_set('memory_limit','1200M');
set_time_limit(900);
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
apache_setenv('no-gzip', '1');

$filename = "test.zip";
$filepath = "http://demo.com/";

// http headers for zip downloads
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
//set_time_limit(0);
ob_clean();
flush();    
readfile($filepath.$filename);
exit;

我的文件大小是 100MB zip 文件。仅下载 45MB 至 50MB。我不知道问题出在哪里。请帮我...

I used code for downloading as follows..

ob_start();
ini_set('memory_limit','1200M');
set_time_limit(900);
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
apache_setenv('no-gzip', '1');

$filename = "test.zip";
$filepath = "http://demo.com/";

// http headers for zip downloads
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
//set_time_limit(0);
ob_clean();
flush();    
readfile($filepath.$filename);
exit;

my file size is 100MB zip file. Only downloading 45MB to 50MB. I dont no where is the problem. please help me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

a√萤火虫的光℡ 2024-12-01 23:36:22

ob_clean 丢弃输出缓冲区的当前内容,但不禁用它。因此,readfile的输出被缓存在内存中,这受到php的memory_limit 指令。

相反,使用 ob_end_clean 丢弃并禁用输出缓冲区,或者根本不使用输出缓冲。

ob_clean discards the current content of the output buffer, but does not disable it. Therefore, the output of readfile is buffered in memory, which is limited by php's memory_limit directive.

Instead, use ob_end_clean to discard and disable the output buffer, or don't use output buffering at all.

上课铃就是安魂曲 2024-12-01 23:36:22

这可能无法解决您的所有问题,但是我看到以下内容:

  1. 输出缓冲。您不需要输出缓冲。删除 ob_start();ob_clean(); 命令。请注意,后者不会破坏输出缓冲区
  2. 取消注释 //set_time_limit(0); 以免遇到时间限制问题。
  3. 启用错误日志记录并从错误日志中了解脚本为什么停止发送文件。

This might not solve all your problems, however I see the following:

  1. output buffering. You don't want output buffering. Remove the ob_start(); and ob_clean(); commands. Note that the latter will not destroy the output buffer.
  2. uncomment //set_time_limit(0); to not run into time limit problems.
  3. enable error logging and learn from the error log why the script stops sending the file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文