X-Sendfile Error
Asked Answered
K

2

0

I am trying to send files by using X-Sendfile directive in lighttpd.

My php code is;

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=" . 's.php');
header("Content-Length: ". filesize("/home/web/domain/htdocs/download.php"));
header( "X-Sendfile: /home/web/domain/htdocs/download.php");

(I am sending download.php file just for testing purpose)

However, I get blank file no matter what I try. If I change the filename, I get;

2010-08-30 18:01:14: (mod_fastcgi.c.2587) send-file error: couldn't get stat_cache entry for: /home/web/domain/htdocs/downloa1d.php

So, it is working, but when I send the correct file it does not give any error in the logs and the browser downloads an empty file.

What could be wrong? What should I do?

Kino answered 30/8, 2010 at 12:51 Comment(5)
Have you tested it without the Content-Length and Content-Type headers?Coxalgia
Wait, you do realize that sending a .php file is supposed to actually stream the contents of the file (and not execute it), right? Also, check the docs. It's X-LIGHTTPD-send_file since X-Sendfile is only supported by 1.5+ (which isn't stable, unless you're running bleeding edge). Also realize that the code you give, and the error you give conflict (They have different paths)...Slicer
Yes I have tested it without the Content-Length and Content-Type headers, in fact I have added them later to try them. And yes, I know, that's why I said (I am sending download.php file just for testing purpose), I will be sending files through our own CDN that is connected to the webservers via a network, so it is just for testing purpose and to isolate the problem. I first though it could be chroot or something. That's why I try to download that file first.Kino
and sorry I forgot to reply the other thing. Yes I was first using X-LIGHTTPD-send-file but changed it. So, it is also not solving the problem. I have also updated lighttpd to 1.4.28 (there is a sendfile bug fixed in 1.4.27) but it is still not working.Kino
Try sending a non-php file. It could be a conflict with the static-file.exclude-extensions settingSlicer
T
1

It is likely that you don't have the right line in your lighttpd.conf file. Add this in your fastcgi.server between the options with => arrows:

"allow-x-send-file" => "enable",

All but the last option end with commas. I assume that you have your fastcgi already configured. If you are using a different framework for connecting PHP to your server, I can't help you. If you have more than one PHP worker process, you must add that option to all of them.

I should add that .php files are small in size and outputting them with readfile() won't make a dent. And, as I see it, you are using some non-critical headers wrongly. There is no such thing as force-download, it will just behave like an unknown file type. Use application/octet-stream. And your Content-Disposition is also shaky. Not only in that weird string enclosing. What the client should get is: Content-Disposition: attachment; filename="My file ♥.txt" The file name is surrounded by double quotes and it is NOT escaped; instead, it is either in UTF-8 or in the encoding of the calling page (this is a dirty grey area where every browser acts differently). You don't even have to include the file name to be on the safe side if you are satisfied with the downloading path. Then, it will look like this: Content-Disposition: attachment You can then use a pseudo-directory trick to supply a less buggy file name: www.example.com/download.php/Custom%20Name.txt This will work more reliably than that filename= parameter, but takes more planning in the link system. It may require you to do some redirecting.

Towne answered 1/4, 2013 at 12:57 Comment(0)
R
1

OK this is an old post, but I'm going to reply anyway 'cause I was stuck on this too for way too long and I didn't find my solution anywhere so here I go:

Yeah I edited lighttpd.conf and added

fastcgi.server += ( ".php" => (( "allow-x-send-file" => "enable" )) )

and used

Header("X-LIGHTTPD-send-file: /path/filename");

in the PHP test page. But it didn't work. It took me a while to figure out I also had a PHP definition in

etc/lighttpd/conf-enabled/15-fastcgi-php.conf

After I added the option in there it worked :)

Ruscio answered 13/12, 2014 at 2:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.