{"record":{"id":"be42711e91965d39","repo":"PHPOffice/PHPWord","slug":"cannot-read-docfile-rtf","errorCode":null,"errorMessage":"Cannot read {$docFile}.","messagePattern":"Cannot read (.+?)\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Reader/RTF.php","lineNumber":48,"sourceCode":"class RTF extends AbstractReader implements ReaderInterface\n{\n    /**\n     * Loads PhpWord from file.\n     *\n     * @param string $docFile\n     *\n     * @return PhpWord\n     */\n    public function load($docFile)\n    {\n        $phpWord = new PhpWord();\n\n        if ($this->canRead($docFile)) {\n            $doc = new Document();\n            $doc->rtf = file_get_contents($docFile);\n            $doc->read($phpWord);\n        } else {\n            throw new Exception(\"Cannot read {$docFile}.\");\n        }\n\n        return $phpWord;\n    }\n}\n","sourceCodeStart":30,"sourceCodeEnd":54,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Reader/RTF.php#L30-L54","documentation":"Thrown by RTF::load() when the generic canRead() guard rejects $docFile. As a sentinel validation error, it indicates the path does not point to a readable RTF document — the file may be missing, unreadable due to permissions, or not an RTF file (canRead inspects the file contents/format). It is not a parse failure; parsing only happens after this guard passes. Callers should confirm the file exists and is RTF before invoking load().","triggerScenarios":"Calling (new RTF())->load($docFile) on a nonexistent path, an unreadable file, or a file that fails RTF detection (e.g. a .docx renamed to .rtf).","commonSituations":"Wrong file extension mapping in an upload handler; saving RTF content as plain text without the RTF header; permissions changed after deployment.","solutions":["Verify the file exists and is a genuine RTF document (starts with {\\rtf)","Use IOFactory::load or IOFactory::createReader so the correct reader is auto-selected","Check permissions and path correctness before load","Catch the exception and report which file failed"],"exampleFix":"// before\n$reader = new RTF();\n$phpWord = $reader->load($file); // may be a docx\n// after\nif (is_file($file) && strpos((string) file_get_contents($file, false, null, 0, 5), '{\\\\rtf') === 0) {\n    $phpWord = (new RTF())->load($file);\n} else {\n    $phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($file);\n}","handlingStrategy":"validation","validationCode":"if (!is_file($docFile) || substr((string) @file_get_contents($docFile, false, null, 0, 5), 0, 5) !== '{\\\\rtf') { throw new \\RuntimeException(\"Not a valid RTF file: $docFile\"); }","typeGuard":null,"tryCatchPattern":"try { $phpWord = (new \\PhpOffice\\PhpWord\\Reader\\RTF())->load($docFile); } catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) { // try IOFactory autodetection }","preventionTips":["Verify the RTF header before load","Use IOFactory::load for automatic reader selection","Never trust file extensions alone"],"tags":["phpword","reader","rtf","file-io"],"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"}