PHPExcel_Writer_Exception 带有消息“无法关闭 zip 文件 php://output”。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21436949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel_Writer_Exception with message "Could not close zip file php://output."
提问by Pavel L
I'm using PHPExcel to export some data to user in an excel file. I would like the script to send the excel file to the user immediately after it's creation. Here is my test code:
我正在使用 PHPExcel 将一些数据导出到 excel 文件中的用户。我希望脚本在创建后立即将 excel 文件发送给用户。这是我的测试代码:
try{
/* Some test data */
$data = array(
array(1, 10 , 2 ,),
array(3, 'qqq', 'some string' ,),
);
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
/* Fill the excel sheet with the data */
$rowI = 0;
foreach($data as $row){
$colI = 0;
foreach($row as $v){
$colChar = PHPExcel_Cell::stringFromColumnIndex($colI++);
$cellId = $colChar.($rowI+1);
$objPHPExcel->getActiveSheet()->SetCellValue($cellId, $v);
}
$rowI++;
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="export.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
}catch(Exception $e){
echo $e->__toString();
}
On my local server (Windows 7 x64, Php 5.3.8, Apache 2.2.21)I get a valid xlsx file. There are no errors.
But there is problem on the live server (Linux 2.6.32-5-amd64, PHP 5.3.3-7+squeeze13, Apache 2.2.16). The script lets the browser to download the "export.xlsx" file with such content:
在我的本地服务器上,(Windows 7 x64, Php 5.3.8, Apache 2.2.21)我得到了一个有效的 xlsx 文件。没有错误。但是直播服务器有问题(Linux 2.6.32-5-amd64, PHP 5.3.3-7+squeeze13, Apache 2.2.16)。该脚本允许浏览器下载包含以下内容的“export.xlsx”文件:
exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file php://output.' in /var/www/someuser/data/www/somedomain.com/libs/PHPExcel/Writer/Excel2007.php:348
Stack trace:
#0 /var/www/someuser/data/www/somedomain.com/classes/Report/Leads/Export.php(339): PHPExcel_Writer_Excel2007->save('php://output')
#1 /var/www/someuser/data/www/somedomain.com/application/pages/account/controllers/TestController.php(13): Report_Leads_Export->Test()
#2 /var/www/someuser/data/www/somedomain.com/libs/Zend/Controller/Action.php(516): Account_TestController->indexAction()
#3 /var/www/someuser/data/www/somedomain.com/libs/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#4 /var/www/someuser/data/www/somedomain.com/libs/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#5 /var/www/someuser/data/www/somedomain.com/index.php(511): Zend_Controller_Front->dispatch()
#6 {main}
PHP is NOT running in Safe Mode. The "open_basedir" option is empty (it's commented out).
PHP 未在安全模式下运行。“open_basedir”选项为空(已注释掉)。
I have found such code in the PHPExcel files:
我在 PHPExcel 文件中找到了这样的代码:
if ($objZip->close() === false) {
throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
}
So the reason of the problem is that $objZip->close() === falsewhere $objZipis an instance of ZipArchiveclass.
所以问题的原因是$objZip->close() === falsewhere$objZip是一个ZipArchive类的实例。
What is the reason of the problem and how can I solve it? Thank you.
问题的原因是什么,我该如何解决?谢谢你。
回答by Mark Baker
The most common cause of this error when saving to php://output is an open_basedir restriction that doesn't include a valid system's temp folder (e.g. /tmp), or permissions for the system's temp folder... suhosin can also affect this, even when the obvious permissions appear to be set correctly.
保存到 php://output 时出现此错误的最常见原因是 open_basedir 限制,该限制不包括有效系统的临时文件夹(例如/tmp),或系统临时文件夹的权限...... suhosin 也会影响这一点,即使当明显的权限似乎设置正确时。
A possible workround is to write the file to the filesystem in a directory that you know you do have full privileges to write, and then use readfile() to stream that file to php://output before deleting the file
一个可能的解决方法是将文件写入文件系统中您知道您有完全写入权限的目录中,然后在删除文件之前使用 readfile() 将该文件流式传输到 php://output
回答by Pavel L
Thanks to Mark Baker. His answer has solved the problem. I have written a simple helper method using his approach.
感谢马克贝克。他的回答解决了这个问题。我使用他的方法编写了一个简单的辅助方法。
static function SaveViaTempFile($objWriter){
$filePath = sys_get_temp_dir() . "/" . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp";
$objWriter->save($filePath);
readfile($filePath);
unlink($filePath);
}
And I have just replaced $objWriter->save('php://output')with SaveViaTempFile($objWriter)
我刚刚替换$objWriter->save('php://output')为SaveViaTempFile($objWriter)
回答by DIEGO ALEXANDER MORA HIDALGO
Hi i tried the following: in a server linux Centos 7.0 do not specified the route of directory tmp, input:
您好,我尝试了以下操作:在服务器 linux Centos 7.0 中不指定目录 tmp 的路径,输入:
function SaveViaTempFile($objWriter){
$filePath = '' . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp";
$objWriter->save($filePath);
readfile($filePath);
unlink($filePath);
exit;
}
and work !!
和工作 !!
回答by Custam
For some people who may have this same ERROR message : it may very simply be because the filename you're trying to save to is actually already open in your Excel.. Was my case
对于某些可能有相同错误消息的人:这可能很简单是因为您尝试保存的文件名实际上已经在您的 Excel 中打开了..是我的情况
回答by blackslifer888
Excelent Friend Work for me in php 7.1.2 and work in PhpSpreadsheet, fix the same file.
优秀的朋友 在 php 7.1.2 中为我工作并在 PhpSpreadsheet 中工作,修复相同的文件。
PhpSpreadsheet/Writer/Excel2007.php
the solution is in de function save in Excel2007.php
解决办法是在de function save in Excel2007.php
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
Replace the second line with this:
用这个替换第二行:
$pFilename = dirname(__FILE__).'/'. rand(0, getrandmax()) . rand(0, getrandmax()) . ".phpxltmp";
thanks.
谢谢。
回答by Giovanny Canasto
I′ve had the same error when I try run my php file, just change the in next line:
当我尝试运行我的 php 文件时,我遇到了同样的错误,只需更改下一行:
$objWriter->save("/dir1"."/".$file.".xlsx");
for this:
为了这:
$objWriter->save(dirname(__FILE__)."/dir1"."/".$file.".xlsx");
add the the dirpath and it worked!!!.
添加dir路径,它起作用了!!!。
回答by Velaro
And my answer is: Filename had symbols such as ":", ",", '"' Had to replace them to different ones. All worked
我的答案是:文件名有符号,如“:”、“、”、“””必须将它们替换为不同的符号。一切正常
回答by Joey Quint
This error happens also when trying to save a file into a folder that doesn't exist. Make sure the whole path exists.
尝试将文件保存到不存在的文件夹时也会发生此错误。确保整个路径存在。
回答by Zipfel
The following works for the Excel2007 format. It would need to be adapted for different formats.
以下适用于 Excel2007 格式。它需要适应不同的格式。
Open this file:
打开这个文件:
\Classes\PHPExcel\Writer\Excel2007.php
Look near Line 196 for:
在 196 号线附近查看:
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
Replace the second line with this:
用这个替换第二行:
$pFilename = dirname(\__FILE__).'/'. rand(0, getrandmax()) . rand(0, getrandmax()) . ".phpxltmp";
Then you can use the export-function as described in the developer guide.
然后您可以使用开发人员指南中描述的导出功能。
回答by Andrés M. Jiménez
In my case, I modify the permissions of the target folder to rwxrwxrwx (0777) and now works!
就我而言,我将目标文件夹的权限修改为 rwxrwxrwx (0777),现在可以使用了!

