php如何提供从远程url下载(流媒体)


How php provide download from remote url (streaming)?

我必须从一个不知道文件大小的远程url提供下载。

到目前为止我遇到的问题

  • 我不知道的文件大小

  • 大约100 mb 的大文件

  • 当我用CURL/readfile/为用户提供下载时。。。然后PHP等待ful文件的时间,然后显示下载窗口。

我如何提供一个部分,流,大块下载?

我想在phpmyadmin上做一些类似的事情——如果你下载了一个sql导出——那么它也会流式传输。

提前谢谢。

流处理通常使用小缓冲区完成。假设您将allow_url_fopen设置为true:

$file = fopen('http://example.com/large-file.bin', 'rb');
while (($content = fread($file, 2048)) !== false) { // Read in 2048-byte chunks
    echo $content; // or output it somehow else.
    flush(); // force output so far
}
fclose($file);