如果您的网络服务器支持,最有效的解决方案是使用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)
好旧的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文档中显示的缓冲区大小,但是根据我的经验,最好提高它,因为您可以以很少的内存使用增加为代价获得有趣的性能提升。
| 归档时间: |
|
| 查看次数: |
3255 次 |
| 最近记录: |