{"record":{"id":"15c4f63e6ef81f39","repo":"PHPOffice/PHPWord","slug":"the-archive-failed-to-load-with-the-following-error-code","errorCode":null,"errorMessage":"The archive failed to load with the following error code: $openStatus","messagePattern":"The archive failed to load with the following error code: \\$openStatus","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Shared/XMLReader.php","lineNumber":71,"sourceCode":"     * @param string $zipFile\n     * @param string $xmlFile\n     *\n     * @return DOMDocument|false\n     */\n    public function getDomFromZip($zipFile, $xmlFile)\n    {\n        if (file_exists($zipFile) === false) {\n            throw new Exception('Cannot find archive file.');\n        }\n\n        $zip = new ZipArchive();\n        $openStatus = $zip->open($zipFile);\n        if ($openStatus !== true) {\n            /**\n             * Throw an exception since making further calls on the ZipArchive would cause a fatal error.\n             * This prevents fatal errors on corrupt archives and attempts to open old \"doc\" files.\n             */\n            throw new Exception(\"The archive failed to load with the following error code: $openStatus\");\n        }\n\n        $content = $zip->getFromName(ltrim($xmlFile, '/'));\n        $zip->close();\n\n        if ($content === false) {\n            return false;\n        }\n\n        return $this->getDomFromString($content);\n    }\n\n    /**\n     * Get DOMDocument from content string.\n     *\n     * @param string $content\n     *\n     * @return DOMDocument","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Shared/XMLReader.php#L53-L89","documentation":"After the file-existence check, XMLReader::getDomFromZip() calls ZipArchive::open(). If it returns anything other than true (an integer ZipArchive error code such as ER_READ=5, ER_NOZIP=19, ER_INCONS=21), further ZipArchive calls would fatal, so the library throws with the numeric error code embedded in the message.","triggerScenarios":"getDomFromZip() given a file that exists but is not a valid zip: old binary .doc saved as .docx, truncated download, password-protected zip, empty/corrupt archive, or a file without read permission (ZipArchive can stat but not open it).","commonSituations":"Users renaming legacy .doc to .docx; interrupted uploads; encrypted office files; disk full or permissions preventing ZipArchive from reading; corrupted templates in storage.","solutions":["Decode the numeric code in the message (e.g. 19 = ZipArchive::ER_NOZIP, 21 = ER_INCONS) to identify the cause.","Validate the archive before use: $z=new ZipArchive(); $z->open($path)===true, or check it's a real OOXML file (finfo mime zip + presence of [Content_Types].xml).","Re-upload/re-download the file if truncated or corrupt; reject legacy .doc files and use the appropriate reader.","Check file permissions and disk space if the file is a valid zip."],"exampleFix":"// before\n$zip = new ZipArchive();\n$status = $zip->open($path); // returns 19, later fatal\n// after\n$zip = new ZipArchive();\n$status = $zip->open($path);\nif ($status !== true) {\n    throw new RuntimeException(\"Corrupt or non-zip document ($status): $path\");\n}","handlingStrategy":"try-catch","validationCode":"$zip = new ZipArchive();\nif ($zip->open($path) !== true) {\n    throw new InvalidArgumentException(\"Not a readable zip archive: $path\");\n}\n$zip->close();","typeGuard":null,"tryCatchPattern":"try {\n    $reader->getDomFromZip($zipFile, $xmlFile);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if (str_contains($e->getMessage(), 'failed to load with the following error code')) {\n        $code = (int) filter_var($e->getMessage(), FILTER_SANITIZE_NUMBER_INT);\n        throw new RuntimeException(\"Corrupt/non-zip document (ZipArchive code $code): $zipFile\", $code, $e);\n    }\n    throw $e;\n}","preventionTips":["Validate zip integrity with ZipArchive::open before processing user-supplied files","Detect actual file type via finfo rather than trusting extensions","Check uploads for completeness (size, checksum) at ingestion","Verify disk space and permissions when archives fail intermittently"],"tags":["phpword","zip","corrupt-file","ziparchive"],"backgroundTag":"unsupported-file-format","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"}