使用nginx和PHPExcel设置内容类型
我正在使用Zend Framework 2开发一个站点,客户希望能够将一些数据导出到Excel文件中。为此,我使用了PHPExcel,并在本地计算机上运行它,在那里我运行Apache2使用nginx和PHPExcel设置内容类型,php,nginx,zend-framework2,apache2,phpexcel,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
<?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;
?>
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
您可以从Apache env和nginx one中转储头文件吗?我现在从这两个环境中添加了头文件。请参阅pastie,我无法复制您的案例:。提供的代码返回提供的标题。我想这不是代码的这一部分,而是一个nginx设置,或者基于您的环境php版本、fw版本等的一些差异。我尝试了您的pastie代码,也得到了正确的标题。这让我走上了另一条道路,我设法解决了这个问题。这是第一次初步的ob_冲洗,这是这个问题的根源。我无法理解为什么这会影响nginx而不是apache。谢谢你花时间帮我解决这个问题
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