PHPOffice/PHPWord · error · Exception

Could not open $filename for reading! File does not exist.

Error message

Could not open $filename for reading! File does not exist.

What it means

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.

Solutions

  1. Verify the file exists with file_exists() before loading
  2. Use an absolute path or __DIR__-based path to avoid working-directory surprises
  3. Check file permissions (is_readable) and fix chmod/chown if needed
  4. Wrap load/canRead in try-catch and show a clear message to the user

Example fix

// before
$phpWord = \PhpOffice\PhpWord\IOFactory::load('doc.docx');
// after
$file = __DIR__ . '/doc.docx';
if (!is_file($file) || !is_readable($file)) {
    throw new RuntimeException("File not readable: $file");
}
$phpWord = \PhpOffice\PhpWord\IOFactory::load($file);
Defensive patterns

Strategy: validation

Validate before calling

if (!is_file($filename) || !is_readable($filename)) { throw new \RuntimeException("Cannot read $filename"); }

Try / catch

try { $phpWord = \PhpOffice\PhpWord\IOFactory::load($filename); } catch (\PhpOffice\PhpWord\Exception\Exception $e) { // show user-friendly file error }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14). Data as JSON: /api/errors/5c108212abec0d9e. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/Reader/AbstractReader.php:101

    public function setImageLoading(bool $value): self
    {
        $this->imageLoading = $value;

        return $this;
    }

    /**
     * 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)
    {

View on GitHub (pinned to aef95c0415)