开发者

X-SendFile on Apache2 (PHP) serving 0B file, no errors though

开发者 https://www.devze.com 2023-03-29 06:55 出处:网络
I installed mod_xsendfile and it seems to have been successful; xsendfile.load appears in /etc/apache2/mods-enabled, and I\'ve found no errors when running my test script. However, every time I run it

I installed mod_xsendfile and it seems to have been successful; xsendfile.load appears in /etc/apache2/mods-enabled, and I've found no errors when running my test script. However, every time I run it, I get served a 0B file.

Here's my test script:

$file = 'sample.mp4';
$path = '/var/storage/media/'.$file;
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("Content-type: application/octet-stream");
header("X-Sendfile: $path");

Obviously I have a file stored in /var/storage/media/sample.mp4, it's only 25MB, and is served perfectly fine if I do it this way:

header('Content-Description: File Transfer');
header('Content-Type: application开发者_StackOverflow/octet-stream');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
exit;

I also have this in the .htaccess file of both /var/storage and /var/www (the files that has all this is stored in /var/www/files/index.php):

XSendFile on
XSendFileAllowAbove on

Like I said, I get no errors, and the PHP can certianly access the file, but I must be missing something with the x-sendfile configuration... that reminds me, I notice in mods-enabled just about every mod has a .load and .conf, but xsendfile only has .load, but so do a few others, so would that have anything to do with it?

Thanks.


I had the same problem and this solution worked for me:

After installing the module, you need to enable it and give an access path into the Apache configuration (httpd.conf for global configuration, or specific site vhosts for specific configuration) like these:

# enable xsendfile
XSendFile On

# enable sending files from parent dirs
XSendFilePath /var/www/

Of course you must replace /var/www/ by whatever folder you want xsendfile to have access to


Make sure you also turn on XSendFile in your Apache configuration if using Apache. If you don't do this, it will look like it works, but empty files will be served.

For example:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    XSendFile on
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>


I'm using it on Ubuntu 14.04 and Apache/2.4.7. It's important to:

  1. Setup on each host configuration:
<Directory /var/www/path/to/files>
    XSendFile On
    XSendFilePath /var/www/path/to/files
</Directory>
  1. Add to .htaccess:
XSendFile on

While I was using only 1; it kept me downloading empty files. After adding 2, worked nicely.


On Windows I had to use the following in order be able to serve files outside of doc roo. Everything else was giving me 404

XSendFilePath C:/


Try making header("X-Sendfile: $path"); the first header() call in the file.

Also take a look at this. Seems like a similar issue and it may give you some ideas:

X-Sendfile Error

0

精彩评论

暂无评论...
验证码 换一张
取 消