xsendFile 下载有 0 个字节

Ivo*_*ema 5 php x-sendfile yii

在 LAMP 堆栈上,我无法让 xSendFile 工作。问题是下载有 0 个字节。

为了确保我安装了 xSendFile,我已将其添加到我的.htaccess文件中:

<IfModule mod_xsendfile.c>
  SetEnv MOD_mod_xsendfile 1
</IfModule>
Run Code Online (Sandbox Code Playgroud)

(我无法测试这个,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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

此代码按预期运行;从中我得出结论,必须安装和/或启用 xSendFile。然后我用 YiixSendFile和普通的 PHP运行这个脚本。在这两种情况下,下载窗口都会打开,但始终为 0 字节。

我没办法。我究竟做错了什么?

为了充分披露,这里是的相关部分httpd.conf

<IfModule mod_xsendfile.c>
    XSendFile on
    XSendFilePath /home/infar
</IfModule>
Run Code Online (Sandbox Code Playgroud)

编辑:

输出压缩关闭:

ini_get("zlib.output_compression"); // returns "Off"
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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;  
Run Code Online (Sandbox Code Playgroud)

这个脚本(我添加的地方echo '12345678901234567890';)生成了一个包含以下 16 个字符串的文件的下载1234567890123456。16 个字符匹配hello.txt. 所以似乎我们可以得出以下结论

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

实际上,注释掉 X-SendFile,如下所示:

// header('X-Sendfile: '.$path.$file);
Run Code Online (Sandbox Code Playgroud)

产生相同的结果;无法从执行X-Sendfile标题中分辨出来。

Mee*_*com 2

确保要提供的文件的路径与您在XSendFilePathonhttpd.conf或 h指令上使用的路径相同ttpd-vhosts.conf

或者:您是否正在发送公共文件夹之外的文件?如果是这样,则将 XSendFileAllowAbove 添加到 apache 配置中