开发者

X-Sendfile Error

开发者 https://www.devze.com 2023-01-13 22:00 出处:网络
I am trying to send files by using X-Sendfile directive in lighttpd. My php code is; header(\"Content-Type: application/force-download\");

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?


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.


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

0

精彩评论

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