I am trying to create a directory with PHP mkdir function but I get an error as follows: Warning: mkdir() [function.mkdir]: Permission denied in ...
. How to settle down the problem?

- 1,201
- 2
- 9
- 3
-
2Platform/web server/operating system? – Salman A Mar 09 '11 at 12:59
-
2check this question out: [http://stackoverflow.com/questions/13908722/php-unable-to-create-a-directory-with-mkdir](http://stackoverflow.com/questions/13908722/php-unable-to-create-a-directory-with-mkdir) I was having the same problem and the answer here just fixed my problem. If you are runnig on linux, the answer there about SELinux might apply to you. – Lucas Madureira Mar 17 '14 at 19:53
-
try this it's working for me https://stackoverflow.com/a/12656964/7516620[enter link description here](https://stackoverflow.com/a/12656964/7516620) – Akshay I Mar 28 '20 at 06:07
15 Answers
I know this is an old thread, but it needs a better answer. You shouldn't need to set the permissions to 777, that is a security problem as it gives read and write access to the world. It may be that your apache user does not have read/write permissions on the directory.
Here's what you do in Ubuntu
Make sure all files are owned by the Apache group and user. In Ubuntu it is the www-data group and user
sudo chown -R www-data:www-data /path/to/webserver/www
Next enabled all members of the www-data group to read and write files
sudo chmod -R g+rw /path/to/webserver/www
The php mkdir()
function should now work without returning errors

- 5,353
- 1
- 35
- 38

- 2,764
- 1
- 17
- 15
-
21[This is a very bad idea](http://www.linuxquestions.org/questions/linux-security-4/which-user-should-own-var-www-873287/page2.html#post4316009). – Big McLargeHuge Jan 05 '15 at 04:55
-
Apache does need permission to write things like cache and logs... it's just a matter of fine tuning. – Manatax Jan 07 '15 at 20:23
-
@simpleengine Hi I've noticed your answer.. I'm facing the same problem with a project I'm trying to create in CodeIgnter and developing in Ubuntu 14.14 Lts.. Could you please explain to me more thoughly the steps I need to do in order to fix this error ??? To give you some additional info: the absolute path is `/opt/lampp/htdocs/www/my-app/public/uploads` .. Basically what I'm trying to do is every logged in user to upload files inside the uploads folder and also create album-folders (this will be done with php) to store photos.. – ltdev Sep 30 '15 at 15:39
-
You can view the topics I have created for additional information http://stackoverflow.com/questions/32861261/codeigniter-message-mkdir-permission-denied-on-ubuntu/32861524#32861524, http://forum.codeigniter.com/thread-63136.html, http://askubuntu.com/questions/679844/mkdir-permission-denied-to-create-folders-with-php – ltdev Sep 30 '15 at 15:39
-
For those using Xampp/Lampp beware Apache seems to run by default as "daemon" user (and group). So instead of: `chown -R www-data:www-data /path/to/webserver/www` Use: `chown -R daemon:daemon /path/to/webserver/www` (or change the user as explained [here](http://stackoverflow.com/questions/25821536/xampp-on-mac-osx-why-running-as-daemon)) – morunas Oct 04 '16 at 12:19
-
If it does not work, the next step is to try asking SELinux to allow Apache to write to the folder `sudo chcon -t httpd_sys_rw_content_t /path/to/folder -R` – giacatho Feb 17 '17 at 03:48
-
29I don't like users who keep saying "it's insecure" without showing the secure way of doing it. – Cedric Ipkiss May 30 '17 at 18:45
-
@simpleengine After using this method, I am not able to use `PHPStorm` to edit the project files. Is there a solution? – Ali Hesari Jun 20 '17 at 18:46
-
Great answer, I had to utilize a little bit of @morunas comment. However, in my case I had to specify phpmyadmin for XAMPP. Here's my solution `sudo chown -R daemon:daemon /opt/lampp/phpmyadmin/` – tisaconundrum Jun 13 '18 at 23:27
-
I just did "chmod -R g+rw /path/to/public_html/app" and now https://example.com/app gives 404, where before it worked. any idea what happen and how I fix it? – Avishay28 Aug 06 '19 at 10:07
-
Does not work, still seeing the error. Edit: I had to flush my project cache (magento 2) – Black Oct 11 '21 at 14:15
Late answer for people who find this via google in the future. I ran into the same problem.
NOTE: I AM ON MAC OSX LION
What happens is that apache is being run as the user "_www" and doesn't have permissions to edit any files. You'll notice NO filesystem functions work via php.
How to fix:
Open a finder window and from the menu bar, choose Go > Go To Folder > /private/etc/apache2
now open httpd.conf
find:
User _www
Group _www
change the username:
User <YOUR LOGIN USERNAME>
Now restart apache by running this form terminal:
sudo apachectl -k restart
If it still doesn't work, I happen to do the following before I did the above. Could be related.
Open terminal and run the following commands: (note, my webserver files are located at /Library/WebServer/www. Change according to your website location)
sudo chmod 775 /Library/WebServer/www
sudo chmod 775 /Library/WebServer/www/*

- 696
- 5
- 8
-
8This solution works, but allows Apache to wreak havoc with your system should someone get it to execute malicious code (e.g. through the Shellshock exploit). An alternative would be to leave Apache executing as `_www` and create a group for you and Apache. Make a new group, say called `trusted`, and put yourself and `_www` in it. Then make trusted the group owner of all the files in your server documents directory. Finally, do `chmod g+rwx` as necessary on only the files Apache needs access to, so that the `trusted` group has permission to edit the files. – Chris Middleton Oct 07 '14 at 21:01
-
4just chiming in to re-emphasize that this answer is a bad idea. there are many very good reasons that apache processes are owned by their own special-purpose user. – danyamachine Dec 13 '15 at 05:50
Don't set permissions to 777 when using mkdir in PHP
Link only answers are not considered good practice on StackOverflow, but the advice that is given here should generally NOT be followed up.
I would like to revert to this great answer on a similar question. I quote:
Please stop suggesting to use 777. You're making your file writeable by everyone, which pretty much means you lose all security that the permission system was designed for. If you suggest this, think about the consequences it may have on a poorly configured webserver: it would become incredibly easy to "hack" the website, by overwriting the files. So, don't.
I know that this thread is old, but perhaps this will help someone, one day.
The problem why PHP says "Permission denied" for mkdir() - wrong url path. So, to fix it, all you need it's to obtain correct path. I did it this way:
<?php
$root = $_SERVER["DOCUMENT_ROOT"];
$dir = $root . '/somefolder/';
if( !file_exists($dir) ) {
mkdir($dir, 0755, true);
}
?>

- 531
- 5
- 12
-
1Happened to me when transferring to a new host (that has mysqlnd lol). I forgot to change out to the new full home directory path. Turns out it wasn't actually a permission problem. – JSG Jan 20 '19 at 20:41
Fix the permissions of the directory you try to create a directory in.

- 88,732
- 13
- 198
- 189
-
I just solved the problem in the original question by changing the file permissions to 755 recursively, using Filezilla. Right click on the parent folder, assuming it is some kind of general file repository folder that it's ok to set 755 permissions on, then File Attributes -> Numeric Value -> 755 , then check "Recurse into sub directories". Takes a couple minutes to propagate through the sub directories if you have a lot of files. Then try again. – TARKUS Apr 03 '16 at 17:08
You need to have file system permission to create the directory.
Example: In Ubuntu 10.04 apache (php) runs as user: www-data in group: www-data
Meaning the user www-data needs access to create the directory.
You can try this yourself by using: 'su www-data' to become the www-data user.
As a quick fix, you can do: sudo chmod 777 my_parent_dir

- 4,118
- 7
- 34
- 53
-
15`chmod 0777` will give read and write access to the world! You should not advise something like that. However, the advise with the user group is currently the best here :) – KingCrunch Mar 09 '11 at 13:17
-
1If the world has access to the OS, sure. The better way would be: sudo chown www-data:www-data my_parent_dir – Jon Skarpeteig Mar 09 '11 at 13:27
-
Or leave the permissions alone and add www-data to the parent directory's group. – Marc B Mar 09 '11 at 14:26
I have this problem just now, my best solution I can give to you right now (despite that you didn't include any of your code) would be:
- Check how you name your destination folder, eg: new_folder (sometimes this can cause error for permission as most hosts don't allow names using underscore, dash, etc to be created at run time). It worked for me.
If you were using recursive command to create sub-folders don't forget to put 0755 (remember to include 0 at the start) to the mkdir command, eg:
if(!file_exists($output)){ if (!mkdir($output, 0755, true)) {//0755 die('Failed to create folders...'); } }
This is also worked for me just now.

- 373
- 2
- 8
This error occurs if you are using the wrong path.
For e.g:
I'm using ubuntu system and my project folder name is 'myproject'
Suppose I have myproject/public/report directory exist and want to create a new folder called orders
Then I need a whole path where my project exists. You can get the path by PWD command
So my root path will be = '/home/htdocs/myproject/'
$dir = '/home/htdocs/myproject/public/report/Orders';
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
It will work for you !! Enjoy !!

- 521
- 5
- 9
What the other thing you can do is go to: /etc/sudoers
There add the following line which gives the permission to that user www-data ALL=(ALL:ALL) ALL Why www-data ? this is because apache is running by this user name.
Incase if your user is different then try username ALL=(ALL:ALL) ALL
This worked for me.

- 539
- 4
- 15
-
6don't edit `sudoers` directly, but log in as root, and use te `visudo` command: that checks any changes to avoid faulty edits, that can cause parse-errors when the sudoers file is loaded, which might leave you powerless (as in no rights to change anything) – Elias Van Ootegem Dec 21 '12 at 13:47
-
How to find the username. like www-data? I am using nginx but not work – robspin Sep 05 '18 at 10:07
-
1
Since you are on a mac, you could add yourself to the _www (the apache user group) group on your mac:
sudo dseditgroup -o edit -a $USER -t user _www
and add _www user to the wheel group which seems to be what the mac creates files as:
sudo dseditgroup -o edit -a _www -t user wheel

- 10,234
- 5
- 32
- 42
If you'r using LINUX, first let's check apache's user, since in some distros it can be different:
egrep -i '^user|^group' /etc/httpd/conf/httpd.conf
Let's say the result is "http". Do the folowwing, just remember change path_to_folder to folders path:
chown -R http:http path_to_folder
chmod -R g+rw path_to_folder

- 1,668
- 2
- 20
- 25
check if its not a issue with umask
if (!function_exists('mkdir_r')) {
/**
* create directory recursively
* @param $dirName
* @param int $rights
* @param string $dir_separator
* @return bool
*/
function mkdir_r($dirName, $rights = 0744, $dir_separator = DIRECTORY_SEPARATOR) {
$dirs = explode($dir_separator, $dirName);
$dir = '';
$created = false;
foreach ($dirs as $part) {
$dir .= $part . $dir_separator;
if (!is_dir($dir) && strlen($dir) > 0) {
$created = mkdir($dir, $rights);
}
}
return $created;
}
}
if (!function_exists('ensure_dir')) {
/**
* ensure directory exist if not create
* @param $dir_path
* @param int $mode
* @param bool $use_mask
* @param int $mask
* @return bool
*/
function ensure_dir($dir_path, $mode = 0744, $use_mask = true, $mask = 0002) {
// set mask
$old_mask = $use_mask && $mask != null
? umask($mask)
: null;
try {
return is_dir($dir_path) || mkdir_r($dir_path, $mode);
} finally {
if ($use_mask && $old_mask != null) {
// restore original
umask($old_mask);
}
}
}
}

- 7,326
- 3
- 36
- 43
After you install the ftp server with sudo apt-get install vsftpd
you will have to configure it. To enable write access you have to edit the /etc/vsftpd.conf
file and uncomment the
#write_enable=YES
line, so it should read
write_enable=YES
Save the file and restart vsftpd
with sudo service vsftpd restart
.
For other configuration options consult this documentation or man vsftpd.conf

- 1,550
- 5
- 32
- 52
I did all I could to fix this issue and none of the above answers helped. Finally restarting apache did the trick
sudo service apache2 restart
My system is running Ubuntu 20 with Samba sharing the directories that had the permission issue.

- 333
- 4
- 17
Try the following on ubuntu 22.04
sudo chown -R ubuntu:ubuntu /var/www/html
sudo chmod -R 755 /var/www/html

- 3,722
- 2
- 38
- 51