{"record":{"id":"1aed3092c2f4ed5f","repo":"PHPOffice/PhpSpreadsheet","slug":"directory-does-not-exist-cachedirectory","errorCode":null,"errorMessage":"Directory does not exist: $cacheDirectory","messagePattern":"Directory does not exist: \\$cacheDirectory","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Writer\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Writer/BaseWriter.php","lineNumber":74,"sourceCode":"        $this->preCalculateFormulas = $precalculateFormulas;\n\n        return $this;\n    }\n\n    public function getUseDiskCaching(): bool\n    {\n        return $this->useDiskCaching;\n    }\n\n    public function setUseDiskCaching(bool $useDiskCache, ?string $cacheDirectory = null): self\n    {\n        $this->useDiskCaching = $useDiskCache;\n\n        if ($cacheDirectory !== null) {\n            if (is_dir($cacheDirectory)) {\n                $this->diskCachingDirectory = $cacheDirectory;\n            } else {\n                throw new Exception(\"Directory does not exist: $cacheDirectory\");\n            }\n        }\n\n        return $this;\n    }\n\n    public function getDiskCachingDirectory(): string\n    {\n        return $this->diskCachingDirectory;\n    }\n\n    protected function processFlags(int $flags): void\n    {\n        if (((bool) ($flags & self::SAVE_WITH_CHARTS)) === true) {\n            $this->setIncludeCharts(true);\n        }\n        if (((bool) ($flags & self::DISABLE_PRECALCULATE_FORMULAE)) === true) {\n            $this->setPreCalculateFormulas(false);","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Writer/BaseWriter.php#L56-L92","documentation":"BaseWriter::setUseDiskCaching(bool $useDiskCache, ?string $cacheDirectory = null) enables temporary-disk caching for writers handling large spreadsheets. The directory must already exist: the setter only validates with is_dir() and throws when the path is missing; it never creates the directory for you.","triggerScenarios":"setUseDiskCaching(true, '/var/cache/xlsx') when that path does not exist; a relative path like 'cache/xlsx' resolved against a different working directory (web SAPI vs CLI); an env-configured path that is empty, misspelled, or points to an unmounted volume.","commonSituations":"Deployments where the cache directory is not part of the image; Docker/Kubernetes volumes not mounted at the expected path; CI environments lacking the app's tmp directory; paths that work locally but differ on the server.","solutions":["Create it before enabling: if (!is_dir($dir)) { mkdir($dir, 0775, true); }","Use an absolute path you control, e.g. sys_get_temp_dir() . '/xlsx-cache'.","Verify both is_dir($dir) and is_writable($dir) and surface a clear configuration error otherwise.","Compare the environment variable or mount actually present in the failing environment against local."],"exampleFix":"// before\n$writer->setUseDiskCaching(true, '/var/cache/xlsx'); // dir absent -> throws\n\n// after\n$dir = '/var/cache/xlsx';\nif (!is_dir($dir)) {\n    mkdir($dir, 0775, true);\n}\n$writer->setUseDiskCaching(true, $dir);","handlingStrategy":"validation","validationCode":"$dir = rtrim((string) (getenv('XLSX_CACHE_DIR') ?: sys_get_temp_dir() . '/xlsx-cache'), '/');\nif (!is_dir($dir)) {\n    mkdir($dir, 0775, true);\n}\nif (!is_writable($dir)) {\n    throw new RuntimeException('Cache directory not writable: ' . $dir);\n}\n$writer->setUseDiskCaching(true, $dir);","typeGuard":null,"tryCatchPattern":"use PhpOffice\\PhpSpreadsheet\\Writer\\Exception as WriterException;\n\ntry {\n    $writer->setUseDiskCaching(true, $dir);\n} catch (WriterException $e) {\n    // disk path unusable: continue with in-memory caching\n    $writer->setUseDiskCaching(false);\n}","preventionTips":["Provision cache directories in deployment (image build, entrypoint), not at request time.","Always pair the two checks: is_dir() and is_writable().","Use absolute paths sourced from one configuration point so CLI and web SAPI agree."],"tags":["phpspreadsheet","writer","disk-cache","filesystem","configuration"],"backgroundTag":"directory-not-found","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}