Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permissions with mkdir won't work

Tags:

I can't understand why I have to use chmod to get the correct permissions.. The file is created succesfully but with 0755 and not 0775 that I specify in mkdir .

( http://php.net/manual/en/function.mkdir.php )

I have to do chmod after mkdir to set the correct permissions.

Safe mode is off in php.ini and the folder belongs to php's group and owner (www-data)

This doesn't work:

  if(!is_dir("/var/www/customers/$username/$project_name"))    {     mkdir("/var/www/customers/$username/$project_name",0775);    } 

But this does:

  if(!is_dir("/var/www/customers/$username/$project_name"))    {     mkdir("/var/www/customers/$username/$project_name");     chmod("/var/www/customers/$username/$project_name",0775);    } 
like image 951
Paris Liakos Avatar asked Jun 03 '11 15:06

Paris Liakos


People also ask

How do I fix permission denied mkdir?

[ErrorException] mkdir(): Permission denied Create a new folder, say 'myproject and run sudo chmod 777 myproject . Then move to 'myproject' folder and create project. To solve this problem, go into your laravel project, make your public directory writable.

Why is mkdir permission denied?

mkdir: cannot create directory – Permission denied The reason for this error is that the user you're running the mkdir as, doesn't have permissions to create new directory in the location you specified. You should use ls command on the higher level directory to confirm permissions.

How do I give permission to mkdir?

How to Set Permissions When Making a Directory. The mkdir command by default gives rwx permissions for the current user only. To add read, write, and execute permission for all users, add the -m option with the user 777 when creating a directory. The directory with rwx permissions for all users is highlighted.

How do you fix mkdir Cannot create directory?

Resolving The Problem Simply log in as super user “su” and use “chmod 777” to set the directory permissions of where you wish the rational directory to be created. Once done, you can re-enter the original directory again and the install will continue using the same directory.


1 Answers

Yes, it's because of umask...

from comments of docs: http://php.net/manual/en/function.mkdir.php

You might notice that when you create a new directory using this code:

mkdir($dir, 0777);

The created folder actually has permissions of 0755, instead of the specified 0777. Why is this you ask? Because of umask(): http://php.net/manual/en/function.umask.php

The default value of umask, at least on my setup, is 18. Which is 22 octal, or 0022. This means that when you use mkdir() to CHMOD the created folder to 0777, PHP takes 0777 and substracts the current value of umask, in our case 0022, so the result is 0755 - which is not what you wanted, probably.

The "fix" for this is simple, include this line:

$old_umask = umask(0);

Right before creating a folder with mkdir() to have the actual value you put be used as the CHMOD. If you would like to return umask to its original value when you're done, use this:

umask($old_umask);

like image 180
confiq Avatar answered Oct 02 '22 19:10

confiq