【问题标题】:Download large file and "connection reset by peer: mod_fcgid (...)"下载大文件和“对等方重置连接:mod_fcgid (...)”
【发布时间】:2015-01-19 11:42:48
【问题描述】:

我正在尝试从网页下载一个大文件 (1.2GB)。当我想下载任何文件时,我应该先登录,我使用 curl 获取 cookie (PHPSESSID),然后我尝试下载这样的文件:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename[1]);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . ($filesize[1]*1000000));
header('Pragma: public');

$opts = array(
  'http'=>array(
    'header'=>"Cookie: PHPSESSID=".$phpSesSid)
);

$context = stream_context_create($opts);
$handle = fopen($dwn, "r", false, $context);
if ($handle)
{
    while (!feof($handle))
    {
        $buffer = fgets($handle, 4096);
        echo $buffer;
        ob_flush();
        flush();
    }
    fclose($handle);
}

这个解决方案很好。该文件正在下载到〜150MB,然后我的浏览器停止下载文件并获取信息:“下载失败”。

我不知道哪里出了问题。当我查看“错误日志”时,我会收到以下错误:

[2015 年 1 月 19 日星期一 12:27:22] [警告] [客户端 178.183.xxx] (104)连接 对等方重置:mod_fcgid:从 FastCGI 服务器读取数据时出错

[2015 年 1 月 19 日星期一 12:27:22] [警告] [客户端 178.183.xxx] (104)连接 对等方重置:mod_fcgid:ap_pass_brigade 在 handle_request_ipc 中失败 功能

[2015 年 1 月 19 日星期一 12:27:22] [错误] [客户端 178.183.xxx] 文件没有 存在:/home/xx/domains/xx.yy.pl/public_html/500.shtml

哪里出了问题?你能帮帮我吗?

谢谢!

【问题讨论】:

    标签: php curl download


    【解决方案1】:

    问题可能是 PHP 脚本被终止了。读取文件然后将其打印到浏览器并不是一件好事,因为 PHP 脚本最多只能运行 30 秒。我想你可以在那个时候下载 150MB,然后脚本被 web 服务器终止。

    您可以通过将浏览器重定向到实际文件来绕过此问题,或者如果您想隐藏实际文件名并直接链接到它,您可以创建一个指向该文件的临时符号链接,然后将您的浏览器重定向到此文件。

    $tempfile = rand();
    symlink($filename, '/www/downloads/'.$tempfile.$filename);
    header('Location: /downloads/'.$tempfile.$filename);
    

    然后设置一个 cron 作业来取消链接早于一天或您希望下载仍然可用的任何时间的符号链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多