fpasstrhu中允许的内存大小耗尽

Sap*_*ena 1 php memory

有没有办法在不超出PHP内存的情况下向浏览器发送一个大的(大约> 700mb)文件?

我尝试使用fpassthrureadfile它超出了内存限制.

Lon*_*ars 8

如果您的网络服务器支持,最有效的解决方案是使用X-Sendfile标头.

这意味着您不需要为提供文件占用PHP,只需发送标头并让Web服务器处理它.

示例(来自Apache mod_xsendfile页面:)

header("X-Sendfile: $path_to_somefile");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$somefile\"");
exit;
Run Code Online (Sandbox Code Playgroud)


Álv*_*lez 5

好旧的fopen()+ fread()+ fclose():

<?php
$handle = fopen('/tmp/foo', 'rb');
while (!feof($handle)) {
    echo fread($handle, 8192);
}
fclose($handle);
Run Code Online (Sandbox Code Playgroud)

8192 是PHP文档中显示的缓冲区大小,但是根据我的经验,最好提高它,因为您可以以很少的内存使用增加为代价获得有趣的性能提升。