PHP mkdir() fails with Permission denied inside a container
Warning: mkdir(): Permission denied in /var/www/html/upload.phpShort answer
mkdir() fails when the user running PHP cannot write to the parent folder. In Docker this is often a folder copied in as root while PHP runs as another user. Check the parent folder's owner against the PHP user first, then open_basedir, then the permissions mkdir gave the new folder.
Fix it fast
1Find out which cause you have
Compare the user PHP runs as with the owner and mode of the parent folder, from inside the container.
docker exec -it my-app sh -c 'id; ls -ld /var/www/html/uploads'2Match what you see, then jump to its fix
- 1
ls -ld shows owner root and mode drwxr-xr-x, while id shows a different user such as www-data.The parent folder belongs to root and PHP runs as another userFix - 2
The warning mentions open_basedir restriction in effect, or the path is outside the folders your configuration allows.open_basedir blocks the pathFix - 3
mkdir() succeeds, but another user or process later gets Permission denied inside the new folder.The new folder got narrower permissions than you expectedFix
- 1
What does the error look like?
Warning: mkdir(): Permission denied in /var/www/html/upload.phpmkdir(): Permission denied
Cause 1 of 3
The parent folder belongs to root and PHP runs as another user
1Check
The mkdir manual lists restricted permissions as a cause of the warning. In a Dockerfile, COPY gives files UID and GID 0 unless you use --chown, and USER sets who the process runs as.
docker exec -it my-app sh -c 'id; ls -ld /var/www/html /var/www/html/uploads'2Fix
Give the folder to the runtime user at build time with COPY --chown, or create it in the image with the right owner.
COPY --chown=www-data:www-data . /var/www/html
USER www-data3Verify
Rebuild, start the container and call mkdir() again.
docker compose up -d --build
docker exec -it my-app php -r "var_dump(mkdir('/var/www/html/uploads/test'));"Confirmed by docs.docker.com
Cause 2 of 3
open_basedir blocks the path
1Check
open_basedir limits file access to the listed directory tree, and a path outside it is refused. Matching is by directory, not by prefix: /www/tmp does not allow /www/tmpfiles.
php -r "var_dump(ini_get('open_basedir'));"2Fix
Add the target directory to open_basedir (separate paths with a colon on Linux), or write inside a folder that is already allowed.
; php.ini
open_basedir = "/var/www/html:/tmp"3Verify
Reload PHP and repeat the mkdir() call.
php -r "var_dump(mkdir('/var/www/html/uploads/test2'));"Confirmed by php.net
Cause 3 of 3
The new folder got narrower permissions than you expected
1Check
The manual says the requested mode is modified by the current umask, and that the mode is ignored on Windows. The folder can therefore end up with fewer permission bits than the mode you passed.
docker exec -it my-app php -r "echo decoct(umask()), PHP_EOL;"
docker exec -it my-app ls -ld /var/www/html/uploads/test2Fix
Create the folder with the mode you need and set the umask only around that call, or use chmod() afterwards.
<?php
$old = umask(0);
mkdir('/var/www/html/uploads/shared', 0775, true);
umask($old);3Verify
List the folder again and compare the mode.
docker exec -it my-app ls -ld /var/www/html/uploads/sharedConfirmed by php.net
How do I stop it coming back?
- Decide the runtime user once (USER in the Dockerfile) and use COPY --chown for every folder it writes to.
- Keep writable folders in one place, such as a dedicated uploads path.
- Pass true as the third argument of mkdir() when parent folders may be missing, and check the return value.
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 built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Back end
PHP says it cannot open a file that is right there
Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php'
PHP could not find the file at the path it built.
2 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
docker build stops at RUN composer install with exit code 1
Allowed memory size of ... bytes exhausted
Exit code 1 only says the RUN step failed.
4 causes, each with a fixBack end
memory_limit does not change inside a PHP Docker container
memory_limit is still 128M after editing php.ini in the container
Your setting is being read from the wrong place or overridden.
4 causes, each with a fixBack end
php artisan serve starts but the app is not reachable, or the .env is ignored
Server stops right after start with a non-zero exit code when --port is given and the port is busy
php artisan serve listens on 127.0.0.1 port 8000 by default.
4 causes, each with a fixBack end
Guzzle or Http::get fails with cURL error 6 in Laravel
Illuminate\Http\Client\ConnectionException: cURL error 6: Could not resolve host
cURL error 6 means the machine running PHP could not turn the host name in the URL into an IP address.
3 causes, each with a fix