PHPOffice/PHPWord · error · Exception
Cannot read .
Error message
Cannot read {$docFile}. What it means
Thrown by RTF::load() when the generic canRead() guard rejects $docFile. As a sentinel validation error, it indicates the path does not point to a readable RTF document — the file may be missing, unreadable due to permissions, or not an RTF file (canRead inspects the file contents/format). It is not a parse failure; parsing only happens after this guard passes. Callers should confirm the file exists and is RTF before invoking load().
Solutions
- Verify the file exists and is a genuine RTF document (starts with {\rtf)
- Use IOFactory::load or IOFactory::createReader so the correct reader is auto-selected
- Check permissions and path correctness before load
- Catch the exception and report which file failed
Example fix
// before
$reader = new RTF();
$phpWord = $reader->load($file); // may be a docx
// after
if (is_file($file) && strpos((string) file_get_contents($file, false, null, 0, 5), '{\\rtf') === 0) {
$phpWord = (new RTF())->load($file);
} else {
$phpWord = \PhpOffice\PhpWord\IOFactory::load($file);
} Defensive patterns
Strategy: validation
Validate before calling
if (!is_file($docFile) || substr((string) @file_get_contents($docFile, false, null, 0, 5), 0, 5) !== '{\\rtf') { throw new \RuntimeException("Not a valid RTF file: $docFile"); } Try / catch
try { $phpWord = (new \PhpOffice\PhpWord\Reader\RTF())->load($docFile); } catch (\PhpOffice\PhpWord\Exception\Exception $e) { // try IOFactory autodetection } Prevention
- Verify the RTF header before load
- Use IOFactory::load for automatic reader selection
- Never trust file extensions alone
When it happens
Trigger: Calling (new RTF())->load($docFile) on a nonexistent path, an unreadable file, or a file that fails RTF detection (e.g. a .docx renamed to .rtf).
Common situations: Wrong file extension mapping in an upload handler; saving RTF content as plain text without the RTF header; permissions changed after deployment.
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
- Cannot read .
- " " is not a valid .
- Could not open $filename for reading! File does not exist.
- Could not open file $filename for reading.
- Invalid value, on of ' . implode(', ', $position) . '…
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/be42711e91965d39.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Reader/RTF.php:48
class RTF extends AbstractReader implements ReaderInterface
{
/**
* Loads PhpWord from file.
*
* @param string $docFile
*
* @return PhpWord
*/
public function load($docFile)
{
$phpWord = new PhpWord();
if ($this->canRead($docFile)) {
$doc = new Document();
$doc->rtf = file_get_contents($docFile);
$doc->read($phpWord);
} else {
throw new Exception("Cannot read {$docFile}.");
}
return $phpWord;
}
}
View on GitHub (pinned to aef95c0415)