xsendFile下载为0字节

问题描述 投票:4回答:3

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

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

实际上,像这样注释掉X-SendFile:

// header('X-Sendfile: '.$path.$file);

产生相同的结果;执行X-Sendfile标头无法区分。

php yii x-sendfile
3个回答
2
投票

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


2
投票

使用纯PHP编辑这部分进行测试:


0
投票

我已经实现了,看上去一切正常,但是Apache用户对位置具有访问权限吗?

© www.soinside.com 2019 - 2024. All rights reserved.