PHPExcel_Writer_Exception with message “Could not close zip file php://output.”

我是研究僧i 提交于 2019-11-27 19:03:29

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.

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

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)

Hi i tried the following: in a server linux Centos 7.0 do not specified the route of directory tmp, input:

function SaveViaTempFile($objWriter){
    $filePath = '' . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp";
    $objWriter->save($filePath);
    readfile($filePath);
    unlink($filePath);
    exit;
}

and work !!

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

Excelent Friend Work for me in php 7.1.2 and work in PhpSpreadsheet, fix the same file.

PhpSpreadsheet/Writer/Excel2007.php

the solution is in 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.

Giovanny Canasto

I´ve had the same error when I try run my php file, just change the in next line:

$objWriter->save("/dir1"."/".$file.".xlsx");

for this:

$objWriter->save(dirname(__FILE__)."/dir1"."/".$file.".xlsx");

add the the dir path and it worked!!!.

And my answer is: Filename had symbols such as ":", ",", '"' Had to replace them to different ones. All worked

In my case, I modify the permissions of the target folder to rwxrwxrwx (0777) and now works!

This error happens also when trying to save a file into a folder that doesn't exist. Make sure the whole path exists.

The following works for the Excel2007 format. It would need to be adapted for different formats.


Open this file:

\Classes\PHPExcel\Writer\Excel2007.php

Look near Line 196 for:

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.

set chmod 777 -R public/results

It's caused by dir permission. Try to enter the final folder, then chmod -R 777 [folder_name]. It should work :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!