编程语言
首页 > 编程语言> > 下载与PHP一起提供的大(ish)压缩邮件对于连接速度慢的人来说已损坏

下载与PHP一起提供的大(ish)压缩邮件对于连接速度慢的人来说已损坏

作者:互联网

我是新手,所以我会尽力解释我遇到的问题.如果我遗漏或不清楚,我会事先道歉.

我在我的根目录之外提供一个81MB的zip文件给那些事先经过验证的人.我一直在收到有关下载损坏或无法完成下载的报告.如果我模拟慢速连接,我已经在我的机器上验证了这一点.

我在运行Apache-Coyote / 1.1的共享主机上.

我收到网络超时错误.我认为我的主持人如果花了太长时间可能会杀死下载,但他们没有以任何方式验证.

我以为我可能遇到内存限制或时间限制,所以我的主机安装了apache模块XSendFile.验证后处理下载的文件中的标题是这样设置的:

<?php
set_time_limit(0);
$file = '/absolute/path/to/myzip/myzip.zip';

header("X-Sendfile: $file");
header("Content-type: application/zip");
header('Content-Disposition: attachment; filename="' . basename($file) . '"');

任何帮助或建议将不胜感激.谢谢!

解决方法:

我建议你看一下这个评论:

http://www.php.net/manual/en/function.readfile.php#99406

特别是,如果你使用的是apache.如果不是,上面链接中的代码应该有用:

I started running into trouble when I had really large files being sent to clients with really slow download speeds. In those cases, the
script would time out and the download would terminate with an
incomplete file. I am dead-set against disabling script timeouts – any
time that is the solution to a programming problem, you are doing
something wrong – so I attempted to scale the timeout based on the
size of the file. That ultimately failed though because it was
impossible to predict the speed at which the end user would be
downloading the file at, so it was really just a best guess so
inevitably we still get reports of script timeouts.

Then I stumbled across a fantastic Apache module called mod_xsendfile ( 07001 (binaries) or
07002 (source)). This module
basically monitors the output buffer for the presence of special
headers, and when it finds them it triggers apache to send the file on
its own, almost as if the user requested the file directly. PHP
processing is halted at that point, so no timeout errors regardless of
the size of the file or the download speed of the client. And the end
client gets the full benefits of Apache sending the file, such as an
accurate file size report and download status bar.

The code I finally ended up with is too long to post here, but in general is uses the mod_xsendfile module if it is present, and if not
the script falls back to using the code I originally posted. You can
find some example code at 07003

编辑

只是为了引用代码来执行“分块”Link to Original Code

<?php 
function readfile_chunked ($filename,$type='array') { 
  $chunk_array=array(); 
  $chunksize = 1*(1024*1024); // how many bytes per chunk 
  $buffer = ''; 
  $handle = fopen($filename, 'rb'); 
  if ($handle === false) { 
   return false; 
  } 
  while (!feof($handle)) { 
      switch($type) 
      { 
          case'array': 
          // Returns Lines Array like file() 
          $lines[] = fgets($handle, $chunksize); 
          break; 
          case'string': 
          // Returns Lines String like file_get_contents() 
          $lines = fread($handle, $chunksize); 
          break; 
      } 
  } 
   fclose($handle); 
   return $lines; 
} 
?>

标签:php,download,zip,shared-hosting,x-sendfile
来源: https://codeday.me/bug/20190704/1374183.html