Make the download of large files with PHP (and lighty) very easy

As you may know, our friend Jan Kneschke is the creator of lighttpd (called „lighty“), a superb OpenSource webserver (designed for speed and easyness) where we provide exclusively consulting and implementation services for.

Some days ago I stumbled upon an old entry of Jan in lighty’s life, called „X-Sendfile“. There he explains how to speed up the delivery of (large) files with lighttpd instead of PHP (YES, lighttpd is very fast – for one customer we created an ImageServer with pure lighty that replaced a 4-server-cluster with Apache and now has 1 server with lighttpd (which is boring around at low load). The box makes 180 Mio. requests per month).

It makes use of the X-Sendfile: (upon lighttpd V1.5) or X-LIGHTTPD-send-file: (prior to V1.5) header lines. In fact, your PHP script does only the following things – for example in a Media Asset Management System:

  • check ACL for the particular user if he has the right to download the file
  • search for the original filename in the database it had when the file was uploaded
  • set the appropriate headers (the file may have a crypting filename on the disk, i.e. not the original filename as it was uploaded for)
  • output file via readfile() and the like
  • set the X-LIGHTTPD-send-file: or X-Sendfile: header
  • exit your script – let lighttpd take care about delivering the file to the client and save your PHP FCGI Binary for the next real application request

As you see, lighty will handle the delivery of the file itself. In order to support the hand-over to lighttpd you need to activate support for X-sendfile in the configuration:

Here’s a short example of a PHP script that lets lighttpd deliver the file:

Happy lightning!

Für neue Blogupdates anmelden:


11 Gedanken zu “Make the download of large files with PHP (and lighty) very easy

  1. wow, that’s a great feature! I’m working on a large-file-download project right now and I wish Apache had one like this, because we can’t just jump over to lighy at the moment …

  2. We just use Akamai for the really big downloads (more of a diskspace issue), as well as Zend Download Server for our regular binary downloads. Then again, I don’t see us running away from Apache any time soon unless there is really something seriously wrong with Apache any time soon, due to the legacy of the business setup.

  3. According to the latest Netcraft survey statistics from 1st of February, 2007, Jan Kneschke’s little baby lighttpd is showing a steady growth, with currently serving 702712 servers which is a tremendous growth compared to January 2007 (only 172819 serve

  4. We are operating heavy traffic website with huge media downloads processed by PHP (because of ACL on each file), which in turn uses our system resources at maximum and even happened that our webserver hangs up few times. If your solution will make our server capable of at least 2x times more requests without any additional costs at all, big THANK YOU!

  5. I wish I could get this to work consistantly. Sometimes I get the files at blazing speed, the next time I get a 500 internal error and an error log entry.
    (mod_fastcgi.c.2462) unexpected end-of-file (perhaps the fastcgi process died).
    I’ve been using Apache & php for years but I’m new to lighttpd. Any suggestions would be appreciated.

  6. Hello,

    I’m using X-LIGHTTPD-send-file on my PHP Script. But before it, i’m changing a $_SESSION variable.

    Unfortunately, it is not working.

    Do you any idea of why it isn’t working?

  7. hi, is possible to serve files hosted on other websites? (like readfile() does)

    for eg.

    header( „Content-Disposition: attachment; filename=\““ . $file_to_download . ‚“‚ );
    Header( „X-LIGHTTPD-send-file: http://otherwebsite.com/videos/funny.flv„);

    ? if not.. how i can do that?! (can i use mod_flv_streaming? how?)

    thank u guys

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.