{"record":{"id":"330b563865cd59f6","repo":"PHPOffice/PHPWord","slug":"could-not-close-zip-file-this-filename","errorCode":null,"errorMessage":"Could not close zip file {$this->filename}: ","messagePattern":"Could not close zip file (.+?): ","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Shared/ZipArchive.php","lineNumber":177,"sourceCode":"\n        return $result;\n    }\n\n    /**\n     * Close the active archive.\n     *\n     * @return bool\n     */\n    public function close()\n    {\n        if (!$this->usePclzip) {\n            try {\n                $result = @$this->zip->close();\n            } catch (Throwable $e) {\n                $result = false;\n            }\n            if ($result === false) {\n                throw new Exception(\"Could not close zip file {$this->filename}: \");\n            }\n        }\n\n        return true;\n    }\n\n    /**\n     * Extract the archive contents (emulate \\ZipArchive).\n     *\n     * @param string $destination\n     * @param array|string $entries\n     *\n     * @return bool\n     *\n     * @since 0.10.0\n     */\n    public function extractTo($destination, $entries = null)\n    {","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Shared/ZipArchive.php#L159-L195","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\n$phpWord->save($lockedFile); // file open in Word -> close fails\n// after\nif (is_writable($lockedFile)) {\n    $phpWord->save($lockedFile);\n}","handlingStrategy":"try-catch","validationCode":"$target = 'output.docx';\nif (!is_writable(dirname($target))) {\n    throw new RuntimeException('Output directory not writable');\n}\nif (file_exists($target) && !is_writable($target)) {\n    throw new RuntimeException('Output file not writable (locked?)');\n}","typeGuard":"function isSaveTargetReady(string $path): bool {\n    return is_writable(dirname($path)) && (!file_exists($path) || is_writable($path));\n}","tryCatchPattern":"try {\n    $phpWord->save($path);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if (str_starts_with($e->getMessage(), 'Could not close zip file')) {\n        // release locks, check permissions, retry\n    }\n}","preventionTips":["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"],"tags":["zip","file-io","filesystem"],"backgroundTag":"file-write-failed","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}