About Application
I am working on an e-commerce application in PHP. To keep URL's secure, product download links are kept behind PHP. There is a file, say download.php, which accepts few parameter via GET and verifies them against a database. If all goes well, it serves file using readfile() function in PHP.
About Problem
Now problem comes when file to be passed to readfile() is larger than memory limit set in php.ini As this application will be used by many users on shared-hosting we cannot relay on altering php.ini settings.
In our effort to find workarounds, I first thought we can go for fread() calls in while loop but it seems that will impose problems as well as highlighted here Downloading large files reliably in PHP
So my best option is to detect/check if server supports X-Accel-Redirect (in case of Nginx) / X-Sendfile (in case of Apache)
If server supports X-Accel-Redirect / X-Sendfile, I can use them and in else block I can make system admin aware about memory limit enforced by php.ini
Ideally, I want to use server side support like X-Accel-Redirect / X-Sendfile wherever possible, and if that doesn't work - I would like to have a fallback code to read files without readfile().
I am not yet sure as how readfile() and fread() in while loop are different but seems while loop will create problem, again, as suggested in Downloading large files reliably in PHP
Hope to get some help, suggestions, codes, guidance.
Thanks for reading.
You could set an environment variable on the server that controls the correct server header name (probably setup by ops team or whoever is responsible for scripting / defining server environment). This would lend itself to any stack supporting Header based file serving because the people in control of that stack are then enabled to make decisions, whilst you just read in from the environment the header.
Warning: It should be noted that this is not a novice move, so be careful. A Lot could go wrong using this if the people in charge of the server environment do not setup it probably won't work or may cause errors, but it's simple, fast and I can't think of a reason to change it.
readfile does not take up a large amount of memory. It opens the file, reads a small portion, writes that portion to the browser and then reuses the memory for the next read. It's the same as using fread+echo in a while loop. You will not be constrained by memory-limits, but you will be limited by max_execution_time and such.
If you want to use X-Accel-Redirect support (or similar) provided by your web server, send a header like this (for Nginx):
Your application cannot know if the server supports this. You will need to provide a configuration option so the admin/installer of your software can provide such information manually.
To detect if the mod_xsendfile apache module installed, you can try this code:
But this code just check if the module installed only, that can cause errors if it's installed but configured wrongly
another possible way to do this to setup server-wide variable through Apache's .htaccess:
and check it form php code:
The common idea is the same for nginx - just pass the value of status variable to backend via HTTP-header or CGI/FastCGI variable.