[php] PHP mkdir: Permission denied problem

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?

This question is related to php

The answer is


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 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:

  1. 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.
  2. 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.


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


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/*

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 !!


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

  1. Make sure all files are owned by the Apache group and user. In Ubuntu it is the www-data group and user

    chown -R www-data:www-data /path/to/webserver/www

  2. Next enabled all members of the www-data group to read and write files

    chmod -R g+rw /path/to/webserver/www

The php mkdir() function should now work without returning errors


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

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);
            }
        }
    }
}

Fix the permissions of the directory you try to create a directory in.


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);
}

?>

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

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


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.