我在我的服务器上设置了一个带有 PHP 的下载脚本,它会在让用户通过 Apache (X-Sendfile) 下载文件之前检查一些细节。文件位于文档根之外。
用Apache和Module X-Sendfile下载的代码是:
header("X-Sendfile: $fullpath");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$link_file\"");
当使用 Apache 和 X-Sendfile 时,我的客户端下载速度为 500 kB/s。我还在 Apache 和没有 X-Sendfile 的情况下使用文档根目录中的相同文件对其进行了测试 - 同样的事情在这里!
所以我在几秒钟后通过带有 readfile 的 PHP 使用相同的客户端、相同的基础设施和相同的互联网连接测试了下载相同的文件:
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)(filesize($fullpath)));
header("Content-Disposition: attachment; filename=\"$link_file\"");
readfile($fullpath);
这次的下载速度是 9.500 kB/s!
我多次使用这两个选项重复此测试,每次尝试的结果都是一样的。在尝试使用 PHP readfile 方法时,除了下载速度之外的唯一区别是几秒钟的等待时间(取决于下载文件的大小)。立即重复PHP readfile方法时,等待时间没有再次出现。很可能是因为它是在第一次之后存储在内存中的。
我在服务器上使用的是专业的 HP Raid-System,其平均本地速度为 800 MB/s,所以这不可能是 Diskspeed 的原因。我也没有在 Apache 的 httpd.conf 中找到任何压缩或带宽设置。
你们中的任何人都可以解释为什么下载速度会有如此大的差异以及如何改变这种差异?
提前谢谢你。
- Server: Windows Server 2008 R2 Apache/2.2.21 (Win32) PHP/5.4.20
- Client: Windows 7 Ultimate x64 Google Chrome 30.0.1599.101 LAN >100 Mbit/s
最佳答案
解决方案:
httpd.conf,开启“EnableSendfile off”这一行
关于php - 通过 Apache 下载速度比使用 PHP readfile 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19601980/