开发者

Does PHP's virtual() function save server memory?

开发者 https://www.devze.com 2023-03-19 12:19 出处:网络
We have sensitive files on a server. We want these to be accessible only to logged in users, so we serve them through a PHP script which checks whether the user has permission to view these files. The

We have sensitive files on a server. We want these to be accessible only to logged in users, so we serve them through a PHP script which checks whether the user has permission to view these files. Then we serve the file with readfile().

This works fine, but it's my understanding that readfile() will load the entire file into memory, and then 开发者_Go百科serve it, while virtual() will serve it directly from PHP, and thus reduce the stress on the server's memory. readfile() is working fine for now, but a new client wants the same functionality with much larger files, and I was wondering whether moving to virtual() would be better.

  1. Is my understanding of readfile() and virtual() correct?
  2. Are there any gotchas with, for example, caching? Or anything else I haven't thought of?


Yes, you are correct. virtual() will make PHP use less memory, but Apache will issue one extra request.

A better way would be the Apache X-Sendfile module. All you'd need to do from PHP is an extra header() call and Apache will serve the file for you, instead of PHP. That uses even less resources.

Here's a tutorial

0

精彩评论

暂无评论...
验证码 换一张
取 消