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

exception phpexcel_writer_exception with message could not close zip file
ziparchive::close(): failure to create temporary file phpexcel
excel php
how to save excel file in folder in php

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:

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:

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).

I have found such code in the PHPExcel files:

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() === false where $objZip is an instance of ZipArchive class.

What is the reason of the problem and how can I solve it? Thank you.

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

How to solve PHPExcel fatal error: Uncaught exception , How to solve PHPExcel fatal error: Uncaught exception '​PHPExcel_Writer_Exception' with message 'Could not close zip'. August 16th 2018  This exception is basically generated when PHPExcel is unable to write to the specifi file or write to the directory that will contain the file, so to troubleshoot this problem you may want to follow these steps: Check that the directory that you are trying to write exists. You may do this with a tool, via CLI or even with a conditional statement in PHP.

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)

Excel2007 Writer : Could not close zip file php://output · Issue #624 , The error message I get (edited the result .xlsx file with notepad++) is : phpexcel-writer-exception-with-message-could-not-close-zip-file-php  Hi, I get following error when storing excel 'PHPExcel_Writer_Exception' with message 'File does not exist' phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel2007

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 !!

PHP Fatal error: Uncaught exception 'PHPExcel_Writer_Exception , error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file 123456789.xlsx.' in /var/www/html/Class Thx in advance BlackPage05 changed the title Uncaught exception 'PHPExcel_Exception' with message 'Invalid cell coordinate' apparently linked to Workbook name PHPExcel_Writer_Excel2007 - Uncaught exception 'PHPExcel_Exception' with message 'Invalid cell coordinate' apparently linked to Workbook name on Aug 11, 2017

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

phpexcel PHPExcel_Writer_Exception with message "Could not , phpexcel PHPExcel_Writer_Exception with message "Could not close zip file php​:output."? A possible workround is to write the file to the filesystem in a directory  Could not open php://output for writing #714. kabeza opened this issue Oct 28, Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not open php

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.

1.4 Fails – error: “could not close zip file php://output”, And when opening a new question, please use the error message (Could not /​phpexcel-writer-exception-with-message-could-not-close-zip-file-php-output,  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Uncaught exception 'PHPExcel_Reader_Exception' with message could not open for reading

PHPExcel_Writer_Exception : Could not close zip file php://output , I have the following message: PHPExcel_Writer_Exception : Could not close zip file php://output. dans PHPExcel_Writer_Excel2007->save()  My educated guess would be that this is due to the fact that the permissions on the folder you're trying to write to are wrong. If that's the case, either make the folder writable (which, from the looks of it would be a bad idea) - or try to save to a location where the web user has write access to solve the problem.

08:25:57 Load from SYLK file 08:25:57 Write to Excel2007 format , Load from SYLK file 08:25:57 Write to Excel2007 format. Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file  Full error: [Thu Apr 14 00:18:09.773306 2016] [:error] [pid 5429] [client 50.46.185.102:56404] PHP Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file 123456789.xlsx.' in /var/www/html/Class

21:59:51 Create new PHPExcel object 21:59:51 Set document , Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message '​Error zipping files : PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open  Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

Comments
  • Suhosin is installed on the live server, but it's not on the local one. So suhosin may cause the problem. But I wouldn't like to see into it. I would like to solve the problem simply using a temp file, as you advice. Thank you!
  • In my case, I have a typo .. :< so make sure you does not have any
  • Hey. Im goin into the same error. I tried to give /tmp and /var/tmp 777 and it does not work. How are you doin that?
  • Is there a solution not saving a temporary file to the filesystem?
  • The first dot (./) ensures that the path is relative to the current working folder; the second dot (export_123.xlsx) is the separator between the filename and the file extension
  • Sounds great, but where do you have to replace this?
  • this was in my case too, so it may happen to others, good to point it out.
  • You should never configure your server directories to be readable and writable by the entire world.
  • This will open up the server to anyone, dont do this.
  • This will open up the server to anyone, do not do this