{"record":{"id":"990859c32565d249","repo":"PHPOffice/PHPWord","slug":"could-not-open-file-filename-for-reading","errorCode":null,"errorMessage":"Could not open file $filename for reading.","messagePattern":"Could not open file \\$filename for reading\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Reader/AbstractReader.php","lineNumber":107,"sourceCode":"\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    {\n        // Check if file exists\n        try {\n            $this->openFile($filename);\n        } catch (Exception $e) {\n            return false;\n        }","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Reader/AbstractReader.php#L89-L125","documentation":"Sentinel failure in AbstractReader::openFile: file_exists() and is_readable() passed but fopen($filename, 'rb') returned false, meaning the file vanished or became unreadable between the check and the open (race, permission change, IO error).","triggerScenarios":"file_exists and is_readable pass but fopen returns false — e.g. race condition where the file is deleted between checks, open_basedir restrictions, too many open file descriptors, or a path that is actually a directory-like special file.","commonSituations":"Server resource exhaustion (ulimit on open files); PHP open_basedir hardening; TOCTOU race in temp-file processing pipelines.","solutions":["Retry opening after a short delay if the file may be in flux","Check open file descriptor limits (ulimit -n) and PHP open_basedir settings","Confirm the path is a regular file, not a fifo/socket/directory","Free other open handles before loading large documents"],"exampleFix":"// before\n$this->fileHandle = fopen($filename, 'rb'); // fails silently elsewhere\n// after\nif (!is_file($filename)) {\n    throw new RuntimeException(\"Not a regular file: $filename\");\n}\n$this->fileHandle = @fopen($filename, 'rb');\nif ($this->fileHandle === false) {\n    throw new RuntimeException(\"fopen failed for $filename\");\n}","handlingStrategy":"try-catch","validationCode":"clearstatcache(true, $filename); if (!is_file($filename)) { throw new \\RuntimeException(\"Not a file: $filename\"); }","typeGuard":null,"tryCatchPattern":"try { $phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($filename); } catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) { if (str_contains($e->getMessage(), 'Could not open file')) { // retry or log resource issue } throw $e; }","preventionTips":["Monitor open file descriptor limits on the server","Check php.ini open_basedir restrictions","Avoid holding many file handles while loading documents"],"tags":["phpword","file-io","fopen-failed"],"backgroundTag":"file-open-failed","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"}