PHPOffice/PHPWord · error · PhpOffice\PhpWord\Exception\Exception
Could not close zip file
Error message
Could not close zip file {$this->filename}: What it means
PhpOffice\PhpWord\Shared\ZipArchive wraps zip archive operations. close() calls the underlying \ZipArchive::close() (suppressing any thrown error) and, if it returns false, throws a PhpWord Exception indicating the zip file could not be finalized. This means buffered changes to the archive could not be written to disk, typically because the underlying ZipArchive object was never opened successfully or the file is not writable.
Solutions
- Close any program holding the target file open, then retry the save
- Verify the output directory exists and is writable (is_writable) before saving
- Check disk free space and permissions on the temp file
- Confirm the archive was opened successfully (check open() return code / ensure ensureZipArchive context) before calling close
Example fix
// before
$phpWord->save($lockedFile); // file open in Word -> close fails
// after
if (is_writable($lockedFile)) {
$phpWord->save($lockedFile);
} Defensive patterns
Strategy: try-catch
Validate before calling
$target = 'output.docx';
if (!is_writable(dirname($target))) {
throw new RuntimeException('Output directory not writable');
}
if (file_exists($target) && !is_writable($target)) {
throw new RuntimeException('Output file not writable (locked?)');
} Type guard
function isSaveTargetReady(string $path): bool {
return is_writable(dirname($path)) && (!file_exists($path) || is_writable($path));
} Try / catch
try {
$phpWord->save($path);
} catch (\PhpOffice\PhpWord\Exception\Exception $e) {
if (str_starts_with($e->getMessage(), 'Could not close zip file')) {
// release locks, check permissions, retry
}
} Prevention
- Never save to a file that a user has open in Word/Excel
- Check is_writable on the output path before saving
- Monitor disk space on servers doing heavy document generation
- Retry with a fresh temp filename if close fails
When it happens
Trigger: Calling close() when the underlying zip was never opened (open() returned an error code that was ignored), the target file is locked by another process (e.g. opened in Word/Excel), or the containing directory lacks write permission. Reached via save operations that use addFileToPackage or when reading helpers (getDomFromZip, getImageString) close the archive.
Common situations: Saving a document while the output file is still open in Word; disk full or read-only filesystem; passing a path to a non-existent directory; antivirus software holding a lock on the temp file on Windows.
Understand the failure class
Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.
Related errors
- Could not close zip file.
- Could not open $filename for reading! File does not exist.
- Could not open file $filename for reading.
- Cannot read .
- Cannot read .
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/330b563865cd59f6.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Shared/ZipArchive.php:177
return $result;
}
/**
* Close the active archive.
*
* @return bool
*/
public function close()
{
if (!$this->usePclzip) {
try {
$result = @$this->zip->close();
} catch (Throwable $e) {
$result = false;
}
if ($result === false) {
throw new Exception("Could not close zip file {$this->filename}: ");
}
}
return true;
}
/**
* Extract the archive contents (emulate \ZipArchive).
*
* @param string $destination
* @param array|string $entries
*
* @return bool
*
* @since 0.10.0
*/
public function extractTo($destination, $entries = null)
{View on GitHub (pinned to aef95c0415)