{"record":{"id":"bfe97c446bceb47f","repo":"PHPOffice/PHPWord","slug":"could-not-open-filename-for-writing","errorCode":null,"errorMessage":"Could not open '{$filename}' for writing.","messagePattern":"Could not open '(.+?)' for writing\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Writer/AbstractWriter.php","lineNumber":285,"sourceCode":"     * @param string $filename\n     *\n     * @return ZipArchive\n     */\n    protected function getZipArchive($filename)\n    {\n        // Remove any existing file\n        if (file_exists($filename)) {\n            unlink($filename);\n        }\n\n        // Try opening the ZIP file\n        $zip = new ZipArchive();\n\n        // @codeCoverageIgnoreStart\n        // Can't find any test case. Uncomment when found.\n        if ($zip->open($filename, ZipArchive::OVERWRITE) !== true) {\n            if ($zip->open($filename, ZipArchive::CREATE) !== true) {\n                throw new \\Exception(\"Could not open '{$filename}' for writing.\");\n            }\n        }\n        // @codeCoverageIgnoreEnd\n\n        return $zip;\n    }\n\n    /**\n     * Open file for writing.\n     *\n     * @since 0.11.0\n     *\n     * @param string $filename\n     *\n     * @return resource\n     */\n    protected function openFile($filename)\n    {","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Writer/AbstractWriter.php#L267-L303","documentation":"getZipArchive could not create/open the package file with ZipArchive::OVERWRITE nor ZipArchive::CREATE after unlinking any existing file, meaning the target path is unwritable (permissions, read-only volume, invalid path) and the save cannot build its ZIP container.","triggerScenarios":"Thrown at src/PhpWord/Writer/AbstractWriter.php:285 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the output directory exists and is writable by the PHP process.","Check disk space and that the path is not on a read-only filesystem.","Ensure the ZipArchive PHP extension is installed and functioning.","Save to a temporary writable path and move the archive into place afterward."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}