XSendfile not working - PHP, Apache, Cpanel

拥有回忆 提交于 2019-12-02 13:29:59

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/8707944/xsendfile-not-working-php-apache-cpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!