{"record":{"id":"f8d3a5d5469affd4","repo":"PHPOffice/PHPWord","slug":"directory-does-not-exist-directory","errorCode":null,"errorMessage":"Directory does not exist: $directory","messagePattern":"Directory does not exist: \\$directory","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Writer/AbstractWriter.php","lineNumber":165,"sourceCode":"    }\n\n    /**\n     * Set use disk caching status.\n     *\n     * @param bool $value\n     * @param string $directory\n     *\n     * @return self\n     */\n    public function setUseDiskCaching($value = false, $directory = null)\n    {\n        $this->useDiskCaching = $value;\n\n        if (null !== $directory) {\n            if (is_dir($directory)) {\n                $this->diskCachingDirectory = $directory;\n            } else {\n                throw new Exception(\"Directory does not exist: $directory\");\n            }\n        }\n\n        return $this;\n    }\n\n    /**\n     * Get disk caching directory.\n     *\n     * @return string\n     */\n    public function getDiskCachingDirectory()\n    {\n        return $this->diskCachingDirectory;\n    }\n\n    /**\n     * Get temporary directory.","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Writer/AbstractWriter.php#L147-L183","documentation":"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.","triggerScenarios":"Calling setUseDiskCaching(true, '/some/path') where /some/path was never created, was deleted, has a typo, or differs between dev and production environments.","commonSituations":"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.","solutions":["Create the directory before enabling disk caching (mkdir($dir, 0775, true)).","Verify the path with is_dir() and realpath() in your bootstrap code.","Call setUseDiskCaching(true) without a directory to use the default location.","Make the path configurable per environment and fall back to memory caching when the dir is absent."],"exampleFix":"// before\n$writer->setUseDiskCaching(true, '/var/cache/phpword'); // dir does not exist -> throws\n// after\n$dir = '/var/cache/phpword';\nif (!is_dir($dir)) { mkdir($dir, 0775, true); }\n$writer->setUseDiskCaching(true, $dir);","handlingStrategy":"validation","validationCode":"function enableDiskCachingSafe(\\PhpOffice\\PhpWord\\Writer\\AbstractWriter $writer, string $dir): void {\n    if (!is_dir($dir)) { mkdir($dir, 0775, true); }\n    if (is_dir($dir) && is_writable($dir)) { $writer->setUseDiskCaching(true, $dir); }\n}","typeGuard":null,"tryCatchPattern":"try {\n    $writer->setUseDiskCaching(true, $cacheDir);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if (str_starts_with($e->getMessage(), 'Directory does not exist')) {\n        $writer->setUseDiskCaching(true); // fall back to default caching location\n    } else { throw $e; }\n}","preventionTips":["Create the cache directory during deployment/bootstrap, not on first use.","Resolve paths with realpath() and verify is_dir() per environment.","Keep cache paths in environment config with sane defaults.","Fall back to memory caching when the directory cannot be prepared."],"tags":["phpword","writer","filesystem","config"],"backgroundTag":"directory-not-found","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}