{"record":{"id":"5c108212abec0d9e","repo":"PHPOffice/PHPWord","slug":"could-not-open-filename-for-reading-file-does-not-exist","errorCode":null,"errorMessage":"Could not open $filename for reading! File does not exist.","messagePattern":"Could not open \\$filename for reading! File does not exist\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Reader/AbstractReader.php","lineNumber":101,"sourceCode":"    public function setImageLoading(bool $value): self\n    {\n        $this->imageLoading = $value;\n\n        return $this;\n    }\n\n    /**\n     * Open file for reading.\n     *\n     * @param string $filename\n     *\n     * @return resource\n     */\n    protected function openFile($filename)\n    {\n        // Check if file exists\n        if (!file_exists($filename) || !is_readable($filename)) {\n            throw new Exception(\"Could not open $filename for reading! File does not exist.\");\n        }\n\n        // Open file\n        $this->fileHandle = fopen($filename, 'rb');\n        if ($this->fileHandle === false) {\n            throw new Exception(\"Could not open file $filename for reading.\");\n        }\n    }\n\n    /**\n     * Can the current ReaderInterface read the file?\n     *\n     * @param string $filename\n     *\n     * @return bool\n     */\n    public function canRead($filename)\n    {","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Reader/AbstractReader.php#L83-L119","documentation":"Generic guard in AbstractReader::openFile fired because file_exists() or is_readable() returned false for $filename before fopen. The named path either does not exist or the process lacks read permission. Thrown to all readers (canRead, save) when given a bad or unreadable path.","triggerScenarios":"Passing a non-existent path to a reader (e.g. new HTML()->load('missing.html')), a path the current process cannot read due to permissions, or an unreadable file via openFile called from canRead/save.","commonSituations":"Wrong working directory so relative paths don't resolve; file deleted between check and use; permission issues after deployment; typo'd path or wrong file extension.","solutions":["Verify the file exists with file_exists() before loading","Use an absolute path or __DIR__-based path to avoid working-directory surprises","Check file permissions (is_readable) and fix chmod/chown if needed","Wrap load/canRead in try-catch and show a clear message to the user"],"exampleFix":"// before\n$phpWord = \\PhpOffice\\PhpWord\\IOFactory::load('doc.docx');\n// after\n$file = __DIR__ . '/doc.docx';\nif (!is_file($file) || !is_readable($file)) {\n    throw new RuntimeException(\"File not readable: $file\");\n}\n$phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($file);","handlingStrategy":"validation","validationCode":"if (!is_file($filename) || !is_readable($filename)) { throw new \\RuntimeException(\"Cannot read $filename\"); }","typeGuard":null,"tryCatchPattern":"try { $phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($filename); } catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) { // show user-friendly file error }","preventionTips":["Use absolute paths built from __DIR__ or config","Check file_exists and is_readable before load","Verify deployment permissions for upload directories"],"tags":["phpword","file-io","missing-file"],"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"}