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.
- Is my understanding of
readfile()
andvirtual()
correct? - 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
精彩评论