PHP warns mkdir(): File exists when the code only needs the folder to be there
Your error
Warning: mkdir(): File exists in /var/www/app/src/Storage.php on line 421 more line it can showFix it fast
Most common cause, 1 of 3The folder already exists and mkdir() runs anyway
Guard the call with is_dir(), as the manual suggests, so mkdir() runs only when the folder is missing.
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}Short answer
Warning: mkdir(): File exists means the path is already taken. PHP's mkdir() treats an existing directory as an error, and the system answers the same way when a file or a symlink, even a dangling one, holds the name. Check with is_dir() first, and tolerate a parallel request creating the folder at the same moment.
Which cause do you have?
Ask the file system what already sits at that path. A directory, a regular file and a symbolic link each lead to a different cause below.
ls -ld /var/www/app/storage/cache
php -r 'var_dump(is_dir($argv[1]), is_file($argv[1]), is_link($argv[1]));' /var/www/app/storage/cacheThen find the line you see in this list and open its fix:
- 1The folder already exists and mkdir() runs anyway
ls -ld prints a line starting with d, and the warning comes back on every run after the first - 2Two processes create the same folder at the same moment
The warning appears only now and then, under load or when several queue workers start together, even though the code checks is_dir() first - 3A file or a symbolic link already holds the name
ls -ld prints a line starting with - (a regular file) or l (a symlink, often to a folder that no longer exists)
What does the error look like?
Warning: mkdir(): File exists in /var/www/app/src/Storage.php on line 42mkdir(): File exists
Cause 1 of 3
The folder already exists and mkdir() runs anyway
You seels -ld prints a line starting with d, and the warning comes back on every run after the first
Check
The PHP manual is explicit that mkdir() emits an E_WARNING when the directory already exists and returns false, because an existing directory counts as an error. Code that calls mkdir() unconditionally, for example in a constructor or a bootstrap file, warns on every request once the folder is there.
grep -rn --exclude-dir=vendor 'mkdir(' src/Fix
Guard the call with is_dir(), as the manual suggests, so mkdir() runs only when the folder is missing.
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}Verify
Run the script twice in a row with errors displayed; the second run prints no warning.
php -d display_errors=1 bin/warm-cache.php && php -d display_errors=1 bin/warm-cache.phpConfirmed by php.net
Cause 2 of 3
Two processes create the same folder at the same moment
You seeThe warning appears only now and then, under load or when several queue workers start together, even though the code checks is_dir() first
Check
A check followed by a create is not atomic. Two PHP processes can both see is_dir() return false, and the slower mkdir() then fails because the faster one has just made the folder, which produces the same File exists warning.
grep -rn --exclude-dir=vendor -A2 'is_dir(' src/ | grep 'mkdir('Fix
Try the create, then confirm the result with a second is_dir(). PHP does not cache stat results for paths that did not exist, so the second check sees the folder the other process made; throw only when neither process managed it.
if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
}Verify
Start the same script several times in parallel; none of them warns or throws.
for i in 1 2 3 4 5; do php bin/warm-cache.php & done; waitConfirmed by php.net
Cause 3 of 3
A file or a symbolic link already holds the name
You seels -ld prints a line starting with - (a regular file) or l (a symlink, often to a folder that no longer exists)
Check
is_dir() returns false for anything that is not a directory, so the guard lets mkdir() run, and the kernel refuses with EEXIST, which PHP shows as File exists. The Linux mkdir(2) manual lists EEXIST for any existing path, not only directories, and names symbolic links, dangling or not, as part of that case.
ls -l /var/www/app/storage/Fix
Remove or rename whatever holds the name, or point the link at a real folder, then create the directory.
# rm on a symlink deletes only the link, not its target
rm /var/www/app/storage/cache
mkdir /var/www/app/storage/cacheVerify
The path is now a directory and the code creates nothing further.
php -r 'var_dump(is_dir($argv[1]));' /var/www/app/storage/cacheConfirmed by man7.org
How do I stop it coming back?
- Create runtime folders once in the deploy script or the image build, not on every request.
- Route all folder creation through one helper that uses the check, create, check-again pattern.
- Log is_file() and is_link() for the path whenever folder creation fails, so a stray file or a broken link is named in the log.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater.