正在下载PHP中的最大文件,最大8192字节? [英] Downloading a large file in PHP, max 8192 bytes?

查看:63
本文介绍了正在下载PHP中的最大文件,最大8192字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码下载大文件(> 100mb).该代码在外壳中执行.

I'm using the following code to download a large file (>100mb). The code is executed in a shell.

$fileHandle = fopen($url, 'rb');
$bytes = 100000;

while ($read = @fread($fileHandle, $bytes)) {
    debug(strlen($read));

    if (!file_put_contents($filePath, $read, FILE_APPEND)) {
        return false;
    }
}

我希望 debug(strlen($ read))会输出 100000 ,这是实际输出:

Where I would expect that debug(strlen($read)) would output 100000, this is the actual output:

10627
8192
8192
8192
...

为什么 fread 第一次读取后的读取次数不超过 8192 个字节,为什么在第一次迭代时读取 10627 个字节?

Why doesn't fread read more than 8192 bytes after the first time, and why does it read 10627 bytes on the first iteration?

这使下载文件非常慢,有更好的方法吗?

This makes downloading the file very slow, is there a better way to do this?

推荐答案

您的问题的答案是(引自PHP文档中的

The answer to your question is (quoting from the PHP docs for fread()):

如果该流被读取缓冲,并且它不表示一个普通文件,则最多读取一次等于该块大小(通常为8192)的字节;根据先前缓冲的数据,返回数据的大小可能大于块大小

if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size

解决性能问题的方法是使用 stream_copy_to_stream(),它应该比使用 fread()进行块读取要快,并且存储效率也更高

The solution to your performance problem is to using stream_copy_to_stream() which should be faster than block reading using fread(), and more memory efficient as well

这篇关于正在下载PHP中的最大文件,最大8192字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆