XSendfile not working - PHP, Apache, Cpanel

2019-03-03 02:42发布

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.

2条回答
叛逆
2楼-- · 2019-03-03 02:58

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.

查看更多
仙女界的扛把子
3楼-- · 2019-03-03 03:05

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

查看更多
登录 后发表回答