Phpexcel_Writer_Exception with Message "Could Not Close Zip File PHP://Output."

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

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

PHPExcel exception: Could not close zip file ...

I was getting the same error "Cannot close zip file.." and realized it didn't have permissions to write to that directory. Check your write permissions. (IIS8 + php + mysql + oracle)

Once i allowed write permissions problem was immediately fixed.

PHPExcel: Exception Could not close zip file on second created file

The error was not caused by PHPExcel but occurred due to control characters (newline and wordwrap) inside the filename. Checking for invisible characters helped me find the characters:

$safeFileTitle = FSHelper::createSafeFileName(substr($arrKitInfo['description_en'], 0, 50));
for ($il=0;$il<strlen($safeFileTitle);$il++) {
print $safeFileTitle[$il].':'.ord($safeFileTitle[$il]).PHP_EOL;
}

I then modified my helper function to remove invisible characters:

  public static function createSafeFileName($fileName) {
// remove all kinds of possibly invalid characters and restrict to characters, digits and whitespace ...
$fileName = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).])", '', $fileName);
// ... and remove control characters
return preg_replace('/[\x00-\x1F\x7F]/', '', $fileName);
}

After the change, the workbooks were created without any problems.

PHPOffice write a new file fatal errors

I found the solution to create dynamic Xlsx files:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="hello.xlsx"');
header('Cache-Control: max-age=0');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

Here, we literally just redirect the browser to the php://output and download the file.

PhpSpreadsheet: Permissions | ZipArchive::close(): Failure to create temporary file

General rules for a directory in PHP to write to it:

It must exist,

It is writable by PHP process,

It is allowed by open_basedir php.ini directive.

Therefore, set some file path as argument in the $writer->save() method and check that these 3 rules are met.

If you want to use only php://output or php://stdout value in the $writer->save() method, check these rules for:

1) The directory returned by the sys_get_temp_dir() function. In Windows sys_get_temp_dir() by default returns the temporary directory of the current OS user. The value can be changed by sys_temp_dir php.ini directive.

or

2) The directory returned by the upload_tmp_dir php.ini directive. $useUploadTempDirectory has false value by default. To set it value to true add this line in your code before saving the file:

\PhpOffice\PhpSpreadsheet\Shared\File::setUseUploadTempDirectory(true);


Here is the code that is responsible for selecting the save path:

From \PhpOffice\PhpSpreadsheet\Writer\Xlsx::save method (source):

// If $pFilename is php://output or php://stdout, make it a temporary file...
$originalFilename = $pFilename;
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
if ($pFilename == '') {
$pFilename = $originalFilename;
}
}

From \PhpOffice\PhpSpreadsheet\SharedFile::sysGetTempDir method (source):

/**
* Get the systems temporary directory.
*
* @return string
*/
public static function sysGetTempDir()
{
if (self::$useUploadTempDirectory) {
// use upload-directory when defined to allow
// running on environments having very restricted open_basedir configs
if (ini_get('upload_tmp_dir') !== false) {
if ($temp = ini_get('upload_tmp_dir')) {
if (file_exists($temp)) {
return realpath($temp);
}
}
}
}
return realpath(sys_get_temp_dir());
}

Fatal error: Uncaught exception 'Exception' in PHPExcel classes

I hope this exception may occur because of the Excel sheet is still in open state. also please check with the permission of the folder in which you write/save/upload the .xlsx file.



Related Topics



Leave a reply



Submit