PHPOffice/PHPWord · error · Exception

Cannot read .

Error message

Cannot read {$docFile}.

What it means

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().

Solutions

  1. Ensure the path points to a real, readable HTML file
  2. Validate with file_exists/is_readable before calling load
  3. If you have an HTML string, write it to a temp file first (or use the appropriate API)
  4. Catch the exception and surface a user-friendly message

Example fix

// before
$phpWord = (new HTML())->load($htmlString); // string, not a path
// after
$tmp = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($tmp, $htmlString);
$phpWord = (new HTML())->load($tmp);
Defensive patterns

Strategy: validation

Validate before calling

if (!is_file($docFile) || !is_readable($docFile)) { throw new \RuntimeException("HTML file not readable: $docFile"); }

Try / catch

try { $phpWord = (new \PhpOffice\PhpWord\Reader\HTML())->load($docFile); } catch (\PhpOffice\PhpWord\Exception\Exception $e) { // fallback or error page }

Prevention

When it happens

Trigger: Calling (new HTML())->load($docFile) with a missing file, a path without read permission, or a file that canRead() rejects (e.g. wrong format).

Common situations: Misconfigured template directory; uploading pipeline wrote an empty or partial file; passing an HTML string instead of a file path.

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/73a0d63c80dbb7c0. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/Reader/HTML.php:47

 */
class HTML extends AbstractReader implements ReaderInterface
{
    /**
     * Loads PhpWord from file.
     *
     * @param string $docFile
     *
     * @return PhpWord
     */
    public function load($docFile)
    {
        $phpWord = new PhpWord();

        if ($this->canRead($docFile)) {
            $section = $phpWord->addSection();
            HTMLParser::addHtml($section, file_get_contents($docFile), true);
        } else {
            throw new Exception("Cannot read {$docFile}.");
        }

        return $phpWord;
    }
}

View on GitHub (pinned to aef95c0415)