About Application

I'm focusing on an e-commerce application in PHP. To help keep URL's secure, product download links are stored behind PHP. There's personal files, say download.php, which accepts couple of parameter via GET and certifies them against a database. If all goes well, it serves file using readfile() function in PHP.

About Problem

Now problem comes when file to become passed to readfile() is bigger than memory limit occur php.ini Because this application will be utilised by many customers on shared-hosting we can't relay on changing php.ini configurations.

Within our effort to locate workarounds, When i first thought we might opt for fread() calls all the while loop however it appears which will impose problems in addition to outlined here Delivering large files dependably in PHP

So my smartest choice would be to identify/see if server supports X-Accel-Redirect (just in case of Nginx) / X-Sendfile (just in case of Apache)

If server supports X-Accel-Redirect / X-Sendfile, I'm able to rely on them as well as in else block I'm able to make system admin aware of memory limit enforced by php.ini

Ideally, I wish to use server side support like X-Accel-Redirect / X-Sendfile whenever we can, and when that does not work - I must possess a fallback code to see files without readfile().

I'm not sure as how readfile() and fread() all the while loop will vary but appears while loop can create problem, again, as recommended in Delivering large files dependably in PHP

Aspire to acquire some help, suggestions, codes, guidance.

Thank you for reading through.

To identify when the mod_xsendfile apache module installed, you can test this code:

if function_exists('apache_get_modules') 
      && in_array('mod_xsendfile', apache_get_modules()) { 

But this code just see if the module installed only, that induce errors whether it's installed but set up wrongly

another possible method of doing this to create server-wide variable through Apache's .htaccess:

<IfModule mod_xsendfile.c>
  <Files *.php>
    XSendFile On
    XSendFileAllowAbove On

and appearance it form php code:


The most popular idea is identical for nginx - just pass the need for status variable to after sales via HTTP-header or CGI/FastCGI variable.

readfile doesn't occupy a lot of memory. It opens the file, reads a little portion, creates that portion towards the browser after which reuses the memory for the following read. It's just like using fread+echo shortly loop. You won't be restricted by memory-limits, but you'll be restricted to max_execution_some time and such.

If you wish to use X-Accel-Redirect support (or similar) supplied by your internet server, send a header such as this (for Nginx):

header('X-Accel-Redirect: /path/to/file');

The application cannot determine if the server supports this. You will have to give a configuration option therefore the admin/installer of the software can offer similarly info by hand.