使用nginx和PHPExcel设置内容类型

时间:2014-05-11 10:43:46

标签: php nginx zend-framework2 apache2 phpexcel

我正在使用 Zend Framework 2 开发网站,其中客户端希望能够将某些数据导出到Excel文件。为此,我选择了 PHPExcel ,然后我在本地计算机上运行它,我在那里运行Apache2。

<?php
        //Create a writer for Excel
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

        // Get the data from php://output
        // https://stackoverflow.com/questions/16551375/phpexcel-in-zend2-controller
        ob_flush();
        ob_start();
        $objWriter->save('php://output');
        $excelOutput = ob_get_clean();

        //Get a new response object
        $response = new \Zend\Http\Response();

        //Set headers for the response object
        $response->getHeaders()
            ->addHeaderLine('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT')
            ->addHeaderLine('Cache-Control: no-store, no-cache, must-revalidate')
            ->addHeaderLine('Cache-Control: post-check=0, pre-check=0')
            ->addHeaderLine('Pragma: no-cache')
            ->addHeaderLine('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
            ->addHeaderLine('Content-Disposition: attachment;filename="report.xlsx"');

        //Set the content for the response object
        $response->setContent($excelOutput);

        return $response;
?>

问题在于,当我将项目上传到生产服务器时,使用 PHP-FPM 以非常标准的设置运行 Nginx , Excel文件未使用正确的标头发送。看起来他们被#34; text / html&#34;这使浏览器显示一些乱码,而不是让文件可供下载。

我按照这个(PHPExcel in Zend2 Controller)问题将内容转换为变量。

我无法弄清楚为什么会发生这种情况,使用 Apache 上的 PHP 而不是使用 Nginx 设置标头的方式不同 PHP-FPM


更新:返回标题

本地计算机上的Apache:

HTTP/1.1 200 OK
Date: Sun, 11 May 2014 11:24:09 GMT
Server: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19
X-Powered-By: PHP/5.4.19
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Last-Modified: Sun, 11 May 2014 11:24:09 GMT
Content-Disposition: attachment;filename="report.xlsx"
Content-Length: 6551
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Nginx标题:

HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Sun, 11 May 2014 11:14:18 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.10-1ubuntu3.11
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip

0 个答案:

没有答案