下载大文件和“通过peer重置连接:mod_fcgid(...)”

时间:2015-01-19 11:42:48

标签: php curl download

我试图从网页下载大文件(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,然后我的浏览器停止下载文件并获取信息:"下载失败"。

我不知道问题出在哪里。当我查看"错误日志"我收到了这个错误:

  

[Mon Jan 19 12:27:22 2015] [warn] [client 178.183.xxx](104)Connection   由peer重置:mod_fcgid:从FastCGI服务器读取数据时出错

     

[Mon Jan 19 12:27:22 2015] [warn] [client 178.183.xxx](104)Connection   由peer重置:mod_fcgid:handle_request_ipc中的ap_pass_brigade失败   功能

     

[Mon Jan 19 12:27:22 2015] [错误] [客户端178.183.xxx]文件没有   存在:/home/xx/domains/xx.yy.pl/public_html/500.shtml

哪里有问题?你可以帮帮我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

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

然后设置一个cron作业,以取消链接超过一天的符号链接或您希望下载保持可用的任何时间。

相关问题