PHPOffice/PHPWord · error · Exception
Could not open file $filename for reading.
Error message
Could not open file $filename for reading.
What it means
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).
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
Example fix
// before
$this->fileHandle = fopen($filename, 'rb'); // fails silently elsewhere
// after
if (!is_file($filename)) {
throw new RuntimeException("Not a regular file: $filename");
}
$this->fileHandle = @fopen($filename, 'rb');
if ($this->fileHandle === false) {
throw new RuntimeException("fopen failed for $filename");
} Defensive patterns
Strategy: try-catch
Validate before calling
clearstatcache(true, $filename); if (!is_file($filename)) { throw new \RuntimeException("Not a file: $filename"); } Try / catch
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; } Prevention
- Monitor open file descriptor limits on the server
- Check php.ini open_basedir restrictions
- Avoid holding many file handles while loading documents
When it happens
Trigger: 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.
Common situations: Server resource exhaustion (ulimit on open files); PHP open_basedir hardening; TOCTOU race in temp-file processing pipelines.
Understand the failure class
Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.
Related errors
- Could not open $filename for reading! File does not exist.
- Cannot read .
- Cannot read .
- Invalid value, on of ' . implode(', ', $position) . '…
- Invalid value, on of ' . implode(', ', $restartNumbers) . '…
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/990859c32565d249.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Reader/AbstractReader.php:107
/**
* Open file for reading.
*
* @param string $filename
*
* @return resource
*/
protected function openFile($filename)
{
// Check if file exists
if (!file_exists($filename) || !is_readable($filename)) {
throw new Exception("Could not open $filename for reading! File does not exist.");
}
// Open file
$this->fileHandle = fopen($filename, 'rb');
if ($this->fileHandle === false) {
throw new Exception("Could not open file $filename for reading.");
}
}
/**
* Can the current ReaderInterface read the file?
*
* @param string $filename
*
* @return bool
*/
public function canRead($filename)
{
// Check if file exists
try {
$this->openFile($filename);
} catch (Exception $e) {
return false;
}View on GitHub (pinned to aef95c0415)