作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在 LAMP 堆栈上,我无法让 xSendFile 工作。问题是下载有 0 个字节。
为了确保我安装了 xSendFile,我已将其添加到我的 .htaccess
文件中:
<IfModule mod_xsendfile.c>
SetEnv MOD_mod_xsendfile 1
</IfModule>
(我无法用 apache_get_modules()
测试它,因为我正在将 PHP 作为 fastCGI 运行。)
然后我有以下 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 显示以下 header :
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
header 中分辨出来。
最佳答案
使用纯 PHP 编辑此部分进行测试:
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); // $path.$file outside quotes
header('Content-Length: '.filesize($path.$file)); // Filesize
编辑:
尝试改变:
header('application/octet-stream');
关于php - xsendFile 下载有 0 个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22508375/
我是一名优秀的程序员,十分优秀!