更快的fopen或file_get_contents? [英] faster fopen or file_get_contents?

查看:73
本文介绍了更快的fopen或file_get_contents?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行多个流量很高的网站,根据要求,所有图像都通过image.php?id=IMAGE_ID_HERE下载. 如果您以前曾经做过,那么您将知道该文件将读取文件图像并将其回显到带有特殊标头的浏览器中.

i am running multiple websites with high traffic , as a requirement , all images are downloaded via image.php?id=IMAGE_ID_HERE . If you ever done that before , you know that that file will be reading the file image and echoing it to the browser with special headers .

我的问题是,服务器上的负载很高(150-200),TOP命令显示了image.php的多个实例,因此image.php运行缓慢!

My problem is , the load on the server is very high (150-200) and TOP command shows multiple instances of image.php , so image.php is running slow !

问题可能是fopen在将图像发送到客户端之前将其加载到内存中.如何读取文件并直接通过文件?

the problem probably is fopen loading the image to the memory before sending it to the client. How to read a file and pass it through directly?

谢谢你们

更新

优化代码后,尽可能使用缓存,请创建CDN.几个服务器,同步方法,负载平衡器,不再需要担心请求了:)

After you optimized the code, used caching wherever possible, do create a CDN . couple of servers, sync methods, load balancers and no need to worry about requests anymore :)

推荐答案

fopen和file_get_contents几乎相等

fopen and file_get_contents are nearly equivalent

为了加快一致性,可以使用您可以使用的页面加载

to speed up with consistence the page load you can use

http://www.php.net/fpassthru

或者甚至更好

http://www.php.net/readfile

具有这些功能,文件的内容被直接打印,每个字节一个字节

with those functions, content of file is printed directly, byte per byte

与file_get_contents相反,例如,将整个数据存储在变量

as opposed to file_get_contents, for example, where you store the whole data inside a variable

$var = file_get_contents();

因此,要使它们正常工作,您需要在提供图像的页面中禁用输出缓冲(否则会使readfile()变得毫无意义)

so, to make these work correctly you will need to disable output buffering (otherwise it would make readfile() pointless) in the page that serves the images

希望这会有所帮助!

这篇关于更快的fopen或file_get_contents?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆