小编Phi*_*ilG的帖子

通过Apache下载比使用PHP readfile更慢

我在我的服务器上设置了一个带PHP的下载脚本,它在让用户通过Apache(X-Sendfile)下载文件之前检查一些细节.文件位于Document-Root之外.

使用Apache和Module X-Sendfile下载的代码是:

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

使用Apache和X-Sendfile时,我的客户端下载速度为500 kB/s.我还用Apache测试了它,没有X-Sendfile和Document Root中的相同文件 - 这里也是一样!

所以我测试下载相同的文件,使用相同的客户端,两侧相同的基础设施和几秒钟后通过PHP与readfile连接相同的互联网连接:

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);
Run Code Online (Sandbox Code Playgroud)

这次下载速度为9.500 kB/s!

我使用两个选项多次重复此测试,每次尝试时结果都相同.除了下载速度之外,唯一的区别是在尝试使用PHP readfile方法时,几秒钟的等待时间(取决于下载的文件的大小).当立即重复PHP readfile方法时,等待时间没有再出现.很可能是因为它在第一次存储在内存中.

我在服务器上使用专业的HP Raid系统,其平均本地速度为800 MB/s,因此不能使用Diskspeed.我也没有在Apache的httpd.conf中找到任何压缩或带宽设置.

你们中的任何人都可以解释为什么下载速度和如何改变它有如此大的差异?

先感谢您.

  • 服务器:Windows Server 2008 R2 Apache/2.2.21(Win32)PHP/5.4.20
  • 客户端:Windows 7旗舰版x64谷歌浏览器30.0.1599.101局域网> 100 Mbit/s

php apache performance x-sendfile readfile

2
推荐指数
1
解决办法
3808
查看次数

标签 统计

apache ×1

performance ×1

php ×1

readfile ×1

x-sendfile ×1