{"record":{"id":"5d21a8ec70d4a737","repo":"PHPOffice/PHPWord","slug":"cannot-find-archive-file","errorCode":null,"errorMessage":"Cannot find archive file.","messagePattern":"Cannot find archive file\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Shared/XMLReader.php","lineNumber":61,"sourceCode":"    /**\n     * DOMXpath object.\n     *\n     * @var DOMXpath\n     */\n    private $xpath;\n\n    /**\n     * Get DOMDocument from ZipArchive.\n     *\n     * @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        }","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Shared/XMLReader.php#L43-L79","documentation":"XMLReader::getDomFromZip() opens a OOXML package (.docx/.xlsx-style zip) to extract an XML part. Its first check is file_exists($zipFile); if the archive file does not exist on disk, Exception('Cannot find archive file.') is thrown before any zip handling.","triggerScenarios":"Calling getDomFromZip() (directly or via reader flows like read(), readRelationships(), getRels(), readHeaderFooter()) with a path that does not exist — wrong path, file not yet written, or reading a stream/remote URL that this method does not support.","commonSituations":"Passing a URL or php:// stream to a template loader; template file deleted between upload and processing; working directory mismatch for relative paths; forgetting to save a generated temp file first.","solutions":["Verify the archive exists with file_exists()/is_file() before loading and print the resolved absolute path (realpath()) in logs.","Use absolute paths for the zip file; fix relative-path/working-directory issues.","Download remote templates to a local temp file before passing them.","Check upload handling so the file is stored (move_uploaded_file) before processing."],"exampleFix":"// before\n$template = 'https://example.com/tpl.docx';\n$reader->getDomFromZip($template, 'word/document.xml');\n// after\n$local = sys_get_temp_dir() . '/tpl.docx';\ncopy('https://example.com/tpl.docx', $local);\nif (!file_exists($local)) {\n    throw new RuntimeException(\"Template missing: $local\");\n}\n$reader->getDomFromZip($local, 'word/document.xml');","handlingStrategy":"validation","validationCode":"if (!is_string($zipFile) || !is_file($zipFile)) {\n    throw new InvalidArgumentException(\"Archive does not exist: \" . var_export($zipFile, true));\n}","typeGuard":"function isExistingFile(mixed $path): bool\n{\n    return is_string($path) && is_file($path);\n}","tryCatchPattern":"try {\n    $reader->getDomFromZip($zipFile, $xmlFile);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if ($e->getMessage() === 'Cannot find archive file.') {\n        throw new RuntimeException(\"Document package missing at $zipFile\", 0, $e);\n    }\n    throw $e;\n}","preventionTips":["Store templates with absolute paths; log realpath() on failure","Never pass URLs or stream wrappers where a local file path is required","Confirm upload persistence (move_uploaded_file) before processing"],"tags":["phpword","zip","file-not-found"],"backgroundTag":"file-not-found","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"}