在LAMP堆栈上,我无法使xSendFile工作。问题是下载的文件有0个字节。
为了确保已安装xSendFile,已将其添加到我的.htaccess
文件中:
<IfModule mod_xsendfile.c>
SetEnv MOD_mod_xsendfile 1
</IfModule>
((我无法将PHP作为fastCGI运行,因此无法使用apache_get_modules()
进行测试。)
然后我有以下PHP代码:
if (1 != getenv('MOD_mod_xsendfile'))
{
echo 'mod_xsendfile is not installed';
}
else
{
$path = '/home/infar/';
$file = 'hello.txt';
if (file_exists($path.$file) && strlen($path.$file)>1)
{
if (true) // change to false to go around Yii
{
Yii::app()->request->xSendFile($path.$file,array(
'saveName'=> 'hello.txt',
'mimeType'=> 'plain/text',
'terminate'=> true,
));
}
else
{
//set proper Content-Type
header('Content-Type: plain/text');
//force download box with the filename hello.txt
header('Content-Disposition: attachment;filename=hello.txt');
header('X-Sendfile: $path.$file');
exit;
}
}
else
{
echo 'file not found at: '.$path.$file;
}
}
此代码按预期运行;从中我得出结论,必须安装和/或启用xSendFile。然后,我同时使用Yii xSendFile
和纯PHP运行此脚本。在这两种情况下,都会打开下载窗口,但始终显示0字节。
我尽力而为。 我在做什么错?
为了充分公开,这是httpd.conf
的相关部分:
<IfModule mod_xsendfile.c>
XSendFile on
XSendFilePath /home/infar
</IfModule>
编辑:
关闭输出压缩:
ini_get("zlib.output_compression"); // returns "Off"
Chrome向我显示了以下标题:
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Disposition:attachment;filename=hello.txt
Content-Length:0
Content-Type:plain/text
Date:Sun, 23 Mar 2014 18:22:49 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=2, max=100
Pragma:no-cache
Server:Apache
X-Sendfile:/home/infar/hello.txt
2014年3月26日更新
[测试@gabrieloliveira建议时,我发现了以下内容:
header('Content-Type: plain/text');
header('Content-Disposition: attachment;filename=hello.txt');
header('X-Sendfile: '.$path.$file); // $path.$file outside quotes
header('Content-Length: '.filesize($path.$file)); // Filesize
echo '12345678901234567890';
exit;
此脚本(我在其中添加了echo '12345678901234567890';
)对包含以下16个字符串1234567890123456
的文件进行了下载。 16个字符与hello.txt
的文件大小匹配。因此看来我们可以得出以下结论
实际上,像这样注释掉X-SendFile:
// header('X-Sendfile: '.$path.$file);
产生相同的结果;执行X-Sendfile
标头无法区分。
确保要提供的文件的路径与您在XSendFilePath
或h httpd.conf
的ttpd-vhosts.conf
指令上使用的路径相同
使用纯PHP编辑这部分进行测试:
我已经实现了,看上去一切正常,但是Apache用户对位置具有访问权限吗?