PHPOffice/PHPWord · error · PhpOffice\PhpWord\Exception\Exception

Directory does not exist: $directory

Error message

Directory does not exist: $directory

What it means

AbstractWriter::setUseDiskCaching(true, $directory) validates the optional cache directory with is_dir(). If a directory path is supplied but it does not exist on disk, the writer refuses the configuration and throws rather than silently falling back.

Solutions

  1. Create the directory before enabling disk caching (mkdir($dir, 0775, true)).
  2. Verify the path with is_dir() and realpath() in your bootstrap code.
  3. Call setUseDiskCaching(true) without a directory to use the default location.
  4. Make the path configurable per environment and fall back to memory caching when the dir is absent.

Example fix

// before
$writer->setUseDiskCaching(true, '/var/cache/phpword'); // dir does not exist -> throws
// after
$dir = '/var/cache/phpword';
if (!is_dir($dir)) { mkdir($dir, 0775, true); }
$writer->setUseDiskCaching(true, $dir);
Defensive patterns

Strategy: validation

Validate before calling

function enableDiskCachingSafe(\PhpOffice\PhpWord\Writer\AbstractWriter $writer, string $dir): void {
    if (!is_dir($dir)) { mkdir($dir, 0775, true); }
    if (is_dir($dir) && is_writable($dir)) { $writer->setUseDiskCaching(true, $dir); }
}

Try / catch

try {
    $writer->setUseDiskCaching(true, $cacheDir);
} catch (\PhpOffice\PhpWord\Exception\Exception $e) {
    if (str_starts_with($e->getMessage(), 'Directory does not exist')) {
        $writer->setUseDiskCaching(true); // fall back to default caching location
    } else { throw $e; }
}

Prevention

When it happens

Trigger: Calling setUseDiskCaching(true, '/some/path') where /some/path was never created, was deleted, has a typo, or differs between dev and production environments.

Common situations: Hardcoded cache paths missing on fresh deployments or CI containers; wrong path separators on Windows; document root moved after a migration; permissions or mount points changed so is_dir() fails.

Related errors


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

Appendix: source

Thrown at src/PhpWord/Writer/AbstractWriter.php:165

    }

    /**
     * Set use disk caching status.
     *
     * @param bool $value
     * @param string $directory
     *
     * @return self
     */
    public function setUseDiskCaching($value = false, $directory = null)
    {
        $this->useDiskCaching = $value;

        if (null !== $directory) {
            if (is_dir($directory)) {
                $this->diskCachingDirectory = $directory;
            } else {
                throw new Exception("Directory does not exist: $directory");
            }
        }

        return $this;
    }

    /**
     * Get disk caching directory.
     *
     * @return string
     */
    public function getDiskCachingDirectory()
    {
        return $this->diskCachingDirectory;
    }

    /**
     * Get temporary directory.

View on GitHub (pinned to aef95c0415)