21

I can't seem to find much documentation on X-Sendfile or example code for PHP (there is some rails code).

Anyone used it before and would mind giving a quick snippet of code and a brief description?

hakre
  • 193,403
  • 52
  • 435
  • 836

2 Answers2

31

X-Sendfile is an HTTP header, so you want something like this:

header("X-Sendfile: $filename");

Your web server picks it up if correctly configured. Here's some more details:

http://www.jasny.net/articles/how-i-php-x-sendfile/

kba
  • 19,333
  • 5
  • 62
  • 89
Don Neufeld
  • 22,720
  • 11
  • 51
  • 50
  • I followed the tutorial on the link, but when I download file with mod-xsend I get file size Binary File (0 bytes)... What am I doing wrong? UPDATE: have figured it out myself. I needed to add `#enable sending files from parent dirs` -> `XSendFileAllowAbove On` into my .htaccess file, sice my downloads are out of the webroot folder. – Primoz Rome Mar 29 '13 at 09:45
  • 5
    For future readers: XSendFileAllowAbove has been deprecated; use XSendFilePath – Oli Nov 12 '14 at 13:25
3

If tweaking the web server configuration is not an option, consider PHP's standard readfile() function. It won't be quite as fast as sendfiling, but it will be more widely compatible. Also note that when doing this, you should also send a Content-Type header at the very least.

Garrett Albright
  • 2,844
  • 3
  • 26
  • 45
  • 1
    Please note that if you use `readfile()`, you will have no support for resuming or partial downloads (Request-Range) or client caching (i.e., returning a `304 Not modified` when needed) (or you will need to handle all this yourself with PHP code -- there should be some code snippets around that exactly do that). – Ale Feb 02 '16 at 19:04