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?


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

    sudo 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

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

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


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

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

?>

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