gpt4 book ai didi

php - xsendFile 下载有 0 个字节

转载 作者:可可西里 更新时间:2023-10-31 23:05:34 27 4
gpt4 key购买 nike

在 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 的文件大小。所以看来我们可以得出以下结论

  1. 正确处理内容长度和文件大小。
  2. X-SendFile 没有找到该文件,或者:
  3. X-SendFile 不处理该文件。

其实就是把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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com