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

5

5

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.

Fineable answered 20/8, 2011 at 18:35 Comment(3)
When you say you get no errors does this mean you have tried "tail -f /var/log/apache2/errors.log"Amor
I read through it and found nothing pertaining to the file it's trying to load. Found plenty of entries about it trying to get the favicon file that I haven't created yet though.Fineable
Heh, yeah I get that favicon error tooAmor
D
8

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

Desuetude answered 25/5, 2013 at 6:28 Comment(1)
At first I only tried adding XSendFile On but I had to also specify XSendFilePath because my rewrite rules started acting weird. Thanks!Humility
I
3

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>
Inflatable answered 12/12, 2011 at 16:22 Comment(0)
C
2

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.

Convolvulus answered 27/7, 2016 at 16:56 Comment(0)
F
1

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

Fugal answered 26/8, 2013 at 23:16 Comment(0)
A
0

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

Amor answered 20/8, 2011 at 19:26 Comment(6)
I read through the suggestions and tried some of them (dropping the type header, switching to X-LIGHTTPD-send-file), but same problem.Fineable
You tried moving the X-Sendfile header to the top of the list of headers?Amor
Yup. Added and removed the other headers in various combinations, no change. Well, aside from the file name being "download" when I got rid of the Content-Disposition header.Fineable
Hmmm I'm stumped then, sorry.Amor
Any chance X-Sendfile isn't actually running but doesn't throw any errors? I'm not sure how to check for that.Fineable
If the .load file contains a LoadModule define for x-sendfile and you are not getting any errors in error.log when restarting apache, then chances are it loaded correctly.Amor

© 2022 - 2024 — McMap. All rights reserved.