Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/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.

like image 555
Doug Wollison Avatar asked Aug 20 '11 18:08

Doug Wollison


3 Answers

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

like image 141
Pierre Avatar answered Nov 09 '22 15:11

Pierre


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>
like image 25
imichaeldotorg Avatar answered Nov 09 '22 16:11

imichaeldotorg


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.

like image 2
Dr Kabeza Avatar answered Nov 09 '22 14:11

Dr Kabeza