{"record":{"id":"73a0d63c80dbb7c0","repo":"PHPOffice/PHPWord","slug":"cannot-read-docfile","errorCode":null,"errorMessage":"Cannot read {$docFile}.","messagePattern":"Cannot read (.+?)\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Reader/HTML.php","lineNumber":47,"sourceCode":" */\nclass HTML 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            $section = $phpWord->addSection();\n            HTMLParser::addHtml($section, file_get_contents($docFile), true);\n        } else {\n            throw new Exception(\"Cannot read {$docFile}.\");\n        }\n\n        return $phpWord;\n    }\n}\n","sourceCodeStart":29,"sourceCodeEnd":53,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Reader/HTML.php#L29-L53","documentation":"Thrown by HTML::load() when the generic canRead() guard fails for the given $docFile. This is a sentinel validation error: canRead() only checks that the file exists, is readable, and passes the reader's format check, so the exception fires whenever the path does not point to a readable HTML document (missing file, bad permissions, wrong format, or no @ symbol guard passing). Callers should verify the file exists and is a valid HTML input before calling load().","triggerScenarios":"Calling (new HTML())->load($docFile) with a missing file, a path without read permission, or a file that canRead() rejects (e.g. wrong format).","commonSituations":"Misconfigured template directory; uploading pipeline wrote an empty or partial file; passing an HTML string instead of a file path.","solutions":["Ensure the path points to a real, readable HTML file","Validate with file_exists/is_readable before calling load","If you have an HTML string, write it to a temp file first (or use the appropriate API)","Catch the exception and surface a user-friendly message"],"exampleFix":"// before\n$phpWord = (new HTML())->load($htmlString); // string, not a path\n// after\n$tmp = tempnam(sys_get_temp_dir(), 'html');\nfile_put_contents($tmp, $htmlString);\n$phpWord = (new HTML())->load($tmp);","handlingStrategy":"validation","validationCode":"if (!is_file($docFile) || !is_readable($docFile)) { throw new \\RuntimeException(\"HTML file not readable: $docFile\"); }","typeGuard":null,"tryCatchPattern":"try { $phpWord = (new \\PhpOffice\\PhpWord\\Reader\\HTML())->load($docFile); } catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) { // fallback or error page }","preventionTips":["Confirm the path points to a real HTML file, not a string","Validate uploads fully wrote to disk before reading","Check permissions after deployment"],"tags":["phpword","reader","html","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"}