1

I have been strongly recommended to use XSendfile since we are serving quite large files from our server. The server is running Cpanel. Previously we were using a straight force-download script, which also did not work well in some browsers. Hoping to kill two birds with one stone with XSendfile.

OK so, our host has enabled Xsendfile on our server. I wrote a quick test script:

$file = "/home/deli/central/testfile.doc";
header("X-Sendfile: $file");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
exit;

When I run this, I get the download prompt. But the file that is sent is always 0kb.

A bit of research, seems you need to set up various things in either the apache config file, or an htaccess file. I was also told that it is not a good idea to add it to the apache config, else it may get overwritten on an update. I would rather do it in htaccess ayway, since I don't have direct access to the apache config and I would rather have the control that doing it with htaccess should offer.

IF I can get it to work, of course.

So, I've added the following to an htaccess file:

XSendFile on XSendFilePath /home/deli/central XSendFileAllowAbove On

(The relative path from the script to the central file directory is ../../deli/central)

If I add these lines to the htaccess, and put it in the public_html directory (same directory as the test script), when I then run the test script I get a 500 error. Quick look at the error logs shows:

/home/north/public_html/.htaccess: XSendFilePath not allowed here

Could anyone enlighten me as to anything I might be doing wrong?

ps - I just read that it is much more efficient to do it in the apache config, so the server is not having to crawl through and load all htaccess files. Don't know if this is true or not.

Help is very much appreciated, this is a bit of a showstopper on the project :)

ps I forgot to mention - if I put in a straight force-download into the script, using the same $file path, the file downloads just fine. So the path would seem to be correct.

Edward Williams
  • 307
  • 1
  • 7
  • 18
  • You are trying to serve files from `/home/deli/...` but your `.htaccess` file is in a different path `/home/north/...`. Are you sure that the correct user (running the Apache process) has access to the `/home/deli` path? – leftclickben Feb 06 '13 at 03:36

2 Answers2

3

I hope this will help someone...

I was having this kind of problem: whenever and whatever -> 0 bytes

I solve this moving the

XSendFile On
XSendFilePath /var/1000italy/data/offline

from the virtualHost section

<VirtualHost *:80>

    DocumentRoot "/var/{{ app_name }}/web"
    ServerName {{ app_name }}.dev

    # here was the problem
    XSendFile On
    XSendFilePath /var/1000italy/data/offline

    <Directory "/var/{{ app_name }}/web">
        allow from all
        Options -Indexes
        AllowOverride All
    </Directory>

    ErrorLog /var/log/apache2/{{ app_name }}_error.log
    CustomLog /var/log/apache2/{{ app_name }}_access.log combined

</VirtualHost>

to the directory section

<VirtualHost *:80>

    DocumentRoot "/var/{{ app_name }}/web"
    ServerName {{ app_name }}.dev

    <Directory "/var/{{ app_name }}/web">
        allow from all
        Options -Indexes
        AllowOverride All

        # HERE EVERYTHING WORKS FINE
        XSendFile On
        XSendFilePath /var/1000italy/data/offline
    </Directory>

    ErrorLog /var/log/apache2/{{ app_name }}_error.log
    CustomLog /var/log/apache2/{{ app_name }}_access.log combined

</VirtualHost>

Ciao

dop3
  • 391
  • 1
  • 14
1

If you are getting 0 bytes it could be output compression needs disabled,see here for more. For the XSendFilePath not allowed here error that is a syntax problem with your .htaccess. Check it manually if you can to ensure it is in the right place per the documentation.

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • Hi thanks for answering. Ok I have asked our server admin to add the following to the apache config file - XSendFile On XSendFileAllowAbove On - they say they have done this, but I am still receiving zero byte files no matter what I try. I also try adding those 2 lines to the root .htaccess file. In that file I have the following - Options -Indexes ErrorDocument 404 /index.html ErrorDocument 403 /index.html XSendFile On XSendFileAllowAbove On – Edward Williams Jan 08 '12 at 10:23
  • ..ps also, I have tried using XSendFilePath in the .htaccess but this also gives me a 500 error. – Edward Williams Jan 08 '12 at 10:35
  • Hi - no need for further help on this one it is now sorted out. Thanks for your help. – Edward Williams Jan 09 '12 at 05:01
  • @EdwardWilliams, hi Ed, I am having the same problem. Would you tell me, how did you fix that, please? – Bunkai.Satori Jan 19 '13 at 21:06
  • 3
    Leaving "now sorted out" messages is of little help. Please update your question or answer your own question with the solution. – Michael Morrison Jul 15 '13 at 02:23