{"record":{"id":"97a2de2a44adf32f","repo":"symfony/http-foundation","slug":"session-storage-was-not-able-to-create-directory-s","errorCode":null,"errorMessage":"Session Storage was not able to create directory \"%s\".","messagePattern":"Session Storage was not able to create directory \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"Session/Storage/Handler/NativeFileSessionHandler.php","lineNumber":48,"sourceCode":"     *\n     * @throws \\InvalidArgumentException On invalid $savePath\n     * @throws \\RuntimeException         When failing to create the save directory\n     */\n    public function __construct(?string $savePath = null)\n    {\n        $baseDir = $savePath ??= \\ini_get('session.save_path');\n\n        if ($count = substr_count($savePath, ';')) {\n            if ($count > 2) {\n                throw new \\InvalidArgumentException(\\sprintf('Invalid argument $savePath \\'%s\\'.', $savePath));\n            }\n\n            // characters after last ';' are the path\n            $baseDir = ltrim(strrchr($savePath, ';'), ';');\n        }\n\n        if ($baseDir && !is_dir($baseDir) && !@mkdir($baseDir, 0o777, true) && !is_dir($baseDir)) {\n            throw new \\RuntimeException(\\sprintf('Session Storage was not able to create directory \"%s\".', $baseDir));\n        }\n\n        if ($savePath !== \\ini_get('session.save_path')) {\n            ini_set('session.save_path', $savePath);\n        }\n        if ('files' !== \\ini_get('session.save_handler')) {\n            ini_set('session.save_handler', 'files');\n        }\n    }\n\n    public function clear(): void\n    {\n        $savePath = \\ini_get('session.save_path');\n        if (str_contains($savePath, ';')) {\n            $savePath = ltrim(strrchr($savePath, ';'), ';');\n        }\n\n        $files = new \\RecursiveIteratorIterator(new \\RecursiveDirectoryIterator($savePath, \\FilesystemIterator::SKIP_DOTS), \\RecursiveIteratorIterator::LEAVES_ONLY);","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/symfony/http-foundation/blob/5aea19cd678fa4140f6108406f1096de5e9ed6e4/Session/Storage/Handler/NativeFileSessionHandler.php#L30-L66","documentation":"NativeFileSessionHandler ensures the session base directory exists; when the directory is missing, it attempts a recursive mkdir with 0777 permissions. If mkdir fails and the directory still does not exist, a RuntimeException is thrown because the handler cannot store session files.","triggerScenarios":"Constructing the handler with a $savePath (or ini session.save_path) pointing to a nonexistent directory that cannot be created — typically due to filesystem permissions, a read-only volume, or an invalid parent path.","commonSituations":"Deployments where /var/lib/php/sessions was removed or never created; Docker containers running as non-root with an unwritable path; misconfigured '2;/nonexistent-root/...' save paths; read-only container filesystems.","solutions":["Create the directory manually with correct ownership/permissions (e.g. mkdir -p /var/lib/php/sessions && chown www-data:www-data ...).","Point session.save_path to an existing, writable directory.","Grant the PHP process user write permission on the target directory's parent chain.","Check that the filesystem/volume is writable (not mounted read-only) and the path is valid."],"exampleFix":"// before\n$handler = new NativeFileSessionHandler('/var/lib/php/sessions'); // dir missing, PHP user cannot create it\n\n// after (shell pre-provisioning)\n// mkdir -p /var/lib/php/sessions && chown www-data:www-data /var/lib/php/sessions\n$handler = new NativeFileSessionHandler('/var/lib/php/sessions');","handlingStrategy":"try-catch","validationCode":"$baseDir = $savePath ?? ini_get('session.save_path');\nif ($baseDir && !is_dir($baseDir) && !@mkdir($baseDir, 0777, true) && !is_dir($baseDir)) {\n    throw new \\RuntimeException(\"Cannot create session dir: $baseDir\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    $handler = new NativeFileSessionHandler($savePath);\n} catch (\\RuntimeException $e) {\n    // provision the directory or switch to an alternative handler\n}","preventionTips":["Provision session directories in deployment/Docker images with correct ownership.","Run PHP workers as a user with write access to the save path.","Avoid read-only root filesystems without a writable tmpfs for sessions.","Health-check directory writability at application boot."],"tags":["sessions","filesystem","permissions","mkdir"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"5aea19cd678fa4140f6108406f1096de5e9ed6e4","analyzedAt":"2026-09-13T01:52:22.855Z","contentChangedAt":"2026-09-13T01:52:22.855Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}