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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Deciphering "Could not close zip file php://output" in PHPExcel

Encountering a PHPExcel_Writer_Exception with the message "Could not close zip file php://output" is a classic symptom of environmental differences between local development and production servers. While your local Windows environment generates valid files without issue, the Linux production server fails during the finalization of the ZIP archive stream. The error originates specifically in PHPExcel/Writer/Excel2007.php, where the underlying ZipArchive object reports a failure upon its closure.

This behavior is not caused by PHP running in Safe Mode or restrictive open_basedir settings, as those are confirmed to be inactive or empty on the live server. Instead, the root cause lies in how the output stream is handled during the execution of the script. The ZipArchive::close() method returns false when it cannot finalize the archive, which triggers the exception.

Stream State and Buffer Flushes

The critical detail missing from the error log but present in the code flow is the state of the output buffer at the moment the file generation completes. When you call $objWriter->save('php://output'), PHPExcel writes the binary data of the .xlsx file directly to the standard output stream. However, the process does not end immediately after the last byte of data is written; the script continues to execute until it hits a terminating statement or an explicit flush.

In many web frameworks, including Zend Framework (evident from your stack trace), the response is often finalized by the framework itself after your action returns. If ob_end_flush() has not been called manually before the exception is thrown, or if the framework attempts to close the output stream while PHPExcel is still in a state expecting to finalize the ZIP archive, you hit this specific wall. The ZipArchive class relies on being able to close the underlying file handle cleanly. If the stream is already considered "closed" by the web server or the framework dispatcher before PHPExcel_Writer_Excel2007 attempts to call $objZip->close(), the operation fails.

The Solution: Manual Output Flush

To resolve this on your Linux environment without modifying the core PHPExcel library, you must ensure that the output stream is explicitly flushed and closed before the writer attempts to finalize the archive, or allow the script to terminate cleanly so the framework does not interfere with the stream state.

The most robust approach in a legacy PHP 5.3 setup involving php://output is to manually flush the output buffer immediately after saving. This ensures that the data has been pushed to the client and the internal handles are ready for closure. You can modify your exception handling block or add logic right after the save call:

try {
    // ... data preparation ...

    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');
    
    // Attempt to save to output
    $objWriter->save('php://output');

    // CRITICAL FIX: Ensure the stream is flushed before the script ends
    // This prevents framework-level interference with the ZipArchive closure
    if (ob_get_level()) {
        ob_flush();
    }
    flush();

} catch (Exception $e) {
    echo $e->getMessage();
}

However, simply flushing might not be enough if the underlying ZipArchive instance detects that the file pointer is invalid upon closure. A more direct fix often employed in these scenarios is to ensure the script terminates immediately after the save operation without triggering any framework-level response finalization that might close the stream prematurely.

If you are constrained to a specific version of PHPExcel (likely 1.8.x given the API usage), upgrading the library to a newer version that supports PhpSpreadsheet or handling the output via a temporary file path before sending headers is often the recommended long-term strategy. Saving to /tmp/export.xlsx, reading it back, and then streaming it avoids the complexity of closing a ZIP archive directly into the web server's output stream.

Why Local vs. Production Differs

Your local Windows environment likely handles output buffering differently than the Apache setup on your Linux VPS. On Windows, php://output often behaves more leniently regarding stream closure sequences in older PHP versions. On Linux with Apache, the interaction between the PHP process and the web server's output filter is stricter. The production environment might be flushing the buffer too early or waiting for a signal that closes the stream before the writer can finalize it.

By explicitly managing the output flush and ensuring no intervening code attempts to close php://output before $objZip->close() executes, you align the execution flow with what the ZipArchive implementation expects, resolving the exception on both environments.

Our network