在PHP中读取/下载大文件而不会耗尽内存

时间:2018-05-08 05:33:10

标签: php fopen large-files fread

如何在不进入allowed memory bytes exhausted的情况下在PHP中读取/下载大型文件?

我正在尝试:

$content = null;
while (!feof($file['data'])) {
    $content .= fread($file['data'], 8192);
}

header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"" . $file['real_filename'] . "\"");
header("Content-Length: " . $file['length']);

echo $content;

1 个答案:

答案 0 :(得分:1)

你试过这个吗?

header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"" . $file['real_filename'] . "\"");
header("Content-Length: " . $file['length']);

while (!feof($file['data'])) {
    print fread($file['data'], 8192);
}