Excel2007: PCLZIP throwing errors

Topics: Developer Forum
Jun 28, 2012 at 2:45 PM
Edited Jun 28, 2012 at 4:08 PM

Case: PHP 5.1.6 on Linux, PHPExcel 1.7.7. The PHP installation has no --enable-zip, therefore i'm using 

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

If i send my PHPExcel object to the browser with the Excel5 Writer it works like a charm, it gives a beautiful XLS-file. If i use the Excel2007-writer with the PCLZIP-class enabled, it sends an empty (0kB) XLSX-sheet to the browser.

$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('php://output');

I figured out that PCLZIP generates an error: (by enabling E_ALL and suppressing the headers)

Fatal error: Uncaught exception 'Exception' with message 'Error zipping files : PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file 'pclzip-4fec5ba9056af.tmp' in binary write mode' in /home/site/html/Classes/PHPExcel/Shared/ZipArchive.php:105

The tmp-directory used by PHPExcel (and PCLZIP, if i'm correct) has CHMOD 777: 

echo substr(sprintf('%o', fileperms(PHPExcel_Shared_File::sys_get_temp_dir())), -4);

gives 1777.

The same code does work on my developement server, which runs PHP 5.3.10 on OS X (Darwin) with --enable-zip turned on (but with the PCLZIP-override in the code). It then gives the correct xlsx sheet.

Jul 5, 2012 at 10:41 AM

Took me some time to figure out: i had to edit the pclzip.lib.php in the Shared directory to use a different temporary directory then the default. Apparently in some server configurations the PCLZip library does not work correctly when using the system temp directory. (Which is weird, because i use the PCLZip library in a different part of the website myself, and there it dóes work with the default temp directory.)

However: problem fixed, but the cause is not completely clear to me...

Coordinator
Jul 11, 2012 at 12:08 AM

Testing to see if I can find an environment that replicates this so I can test a fix

Nov 14, 2012 at 12:12 AM
Edited Nov 14, 2012 at 11:21 AM

Hi Mark,

Just had the same issue. Somehow my tmp_dir did not get a trailing slash '/'. There is even a note in the pclzip.lib.php file about it.

I now added the following line to my "config.inc.php" file of my project and all is fine:

define( 'PCLZIP_TEMPORARY_DIR', '/tmp/' );

Hope that helps,

Martijn