无法恢复大于 300M 的下载

发布于 2024-10-13 14:22:18 字数 849 浏览 8 评论 0原文

我正在开发一个用 php 来下载文件的程序。 脚本请求如下:http://localhost/download.php?file=abc.zip 我使用 使用 PHP 发送时可恢复下载中提到的一些脚本文件?

它肯定适用于300M以下的文件,无论是多线程还是单线程下载,但是,当我尝试下载>300M的文件时,我在单线程下载中遇到问题,我只下载了大约< strong>250M数据,那么看起来http连接断开了。它不会在断点处中断..为什么? 调试脚本,我查明了它的问题所在:

$max_bf_size = 10240;
$pf = fopen("$file_path", "rb");
fseek($pf, $offset);
while(1)
{
    $rd_length = $length < $max_bf_size? $length:$max_bf_size;
    $data = fread($pf, $rd_length);
    print $data;
    $length = $length - $rd_length;
    if( $length <= 0 )
    {
        //__break-point__ 
        break;
    }

}

这似乎每个请求的文档只能获取 250M 数据缓冲区来 echoprint..但是当我使用多文件时它可以工作-下载文件的线程

I am working on a program with php to download files.
the script request is like: http://localhost/download.php?file=abc.zip
I use some script mentioned in Resumable downloads when using PHP to send the file?

it definitely works for files under 300M, either multithread or single-thread download, but, when i try to download a file >300M, I get a problem in single-thread downloading, I downloaded only about 250M data, then it seems like the http connection is broken. it doesnot break in the break-point ..Why?
debugging the script, I pinpointed where it broke:

$max_bf_size = 10240;
$pf = fopen("$file_path", "rb");
fseek($pf, $offset);
while(1)
{
    $rd_length = $length < $max_bf_size? $length:$max_bf_size;
    $data = fread($pf, $rd_length);
    print $data;
    $length = $length - $rd_length;
    if( $length <= 0 )
    {
        //__break-point__ 
        break;
    }

}

this seems like every requested document can only get 250M data buffer to echo or print..But it works when i use a multi-thread to download a file

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

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

发布评论

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

评论(1

陪我终i 2024-10-20 14:22:18

fread() 将读取您要求的字节数,因此您正在做一些不必要的工作来计算要读取的字节数。我不知道你说的单线程和多线程下载是什么意思。您知道 readfile() 可以转储整个文件吗?我假设您需要读取从 $offset 开始直到 $length 字节的文件部分,对吗?

我还会检查我的 Web 服务器(Apache?)配置和 ISP 限制(如果适用);您的最大响应大小或时间可能会受到限制。

试试这个:

define(MAX_BUF_SIZE, 10240);
$pf = fopen($file_path, 'rb');
fseek($pf, $offset);
while (!feof($pf)) {
    $data = fread($pf, MAX_BUF_SIZE);
    if ($data === false)
        break;
    print $data;
}
fclose($pf);

fread() will read up to the number of bytes you ask for, so you are doing some unnecessary work calculating the number of bytes to read. I don't know what you mean by single-thread and multi-thread downloading. Do you know about readfile() to just dump an entire file? I assume you need to read a portion of the file starting at $offset up to $length bytes, correct?

I'd also check my web server (Apache?) configuration and ISP limits if applicable; your maximum response size or time may be throttled.

Try this:

define(MAX_BUF_SIZE, 10240);
$pf = fopen($file_path, 'rb');
fseek($pf, $offset);
while (!feof($pf)) {
    $data = fread($pf, MAX_BUF_SIZE);
    if ($data === false)
        break;
    print $data;
}
fclose($pf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文