Create a folder in PHP if it doesn't exist: the evolution of one question
If you've ever written a script that uploads files, saves cache, or generates reports, you've likely encountered the situation: "But the folder doesn't exist!". An error that crashes the entire script, leaving the user staring at a white screen or (even worse) a cryptic PHP error.
The question "Create a folder if it doesn't already exist" on Stack Overflow gathers thousands of views. And for good reason. It seems simple, right? But the devil, as always, is in the details: permissions, recursion, exception handling, folder name encoding. In 2025, as projects become more complex and security requirements stricter, proper directory creation is a fundamental skill every PHP developer must know.
In this article, we'll cover 5 best practices for creating a folder in PHP if it doesn't exist. From a simple solution for beginners to production-grade code with race condition protection. Let's go!
Why plain mkdir() is bad? (a story of one error)
In the beginning, many write this:
<?phpmkdir('/path/to/my/folder');?>And get:
- Warning: mkdir(): No such file or directory — if the parent folder doesn't exist.
- Warning: mkdir(): File exists — if the folder already exists.
Both warnings are not just "yellow text". In production mode, they can turn into fatal errors if strict error reporting is enabled. It's also a security hole: an attacker could create a folder with the same name before your script does (race condition).
Therefore, the modern approach always involves checking, recursion, and permission control.
Method 1: Classic with is_dir() and mkdir() (for beginners)
The simplest and most understandable way is to first check if the folder exists, and then create it.
<?php$folder = '/var/www/uploads/images';
if (!is_dir($folder)) { mkdir($folder, 0755, true); echo "Folder created!";} else { echo "Folder already exists.";}?>What's important here:
is_dir()— returns true if the path exists and is a directory.mkdir($folder, 0755, true)— the third parametertrueenables recursive creation. If the parent folder doesn't exist, PHP will create the entire chain (/var/www/uploads/images→ createsuploads, thenimages).0755— permissions: owner can do everything, others can read and execute.
Downside: this code is not protected against race conditions. If two requests simultaneously check is_dir() and both attempt to create the folder, the second one will trigger a warning.
Method 2: Production approach with @mkdir() and error checking
Experienced developers often use the error suppression operator @ and then explicitly check the result.
<?php$folder = '/var/www/uploads/images';
if (!is_dir($folder)) { // Suppress warning if folder already exists if (!@mkdir($folder, 0755, true) && !is_dir($folder)) { throw new \\RuntimeException('Failed to create folder: ' . $folder); }}?>Why is this better?
- We suppress the warning but don't ignore the error — if creation fails, an exception is thrown.
- The double check
!is_dir($folder)aftermkdir()protects against race conditions: if another process created the folder between our check and themkdir()call, we won't crash.
Note: The @ operator reduces performance because PHP still generates the error but hides it. In high-load projects, it's better to use method 3.