Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't PHP create a directory with 777 permissions?

Tags:

php

mkdir

I'm trying to create a directory on my server using PHP with the command:

mkdir("test", 0777); 

But it doesn't give full permissions, only these:

rwxr-xr-x 
like image 798
Sjwdavies Avatar asked Oct 22 '10 14:10

Sjwdavies


People also ask

How do I give PHP permission to 777?

You can use chmod() to do this.

What does chmod 777 do to a directory?

chmod 777: Everything for everyone You might have heard of chmod 777. This command will give read, write and execute permission to the owner, group and public. chmod 777 is considered potentially dangerous because you are giving read, write and execute permission on a file/directory to everyone (who is on your system).

What permissions should php files have?

Set php files to 640. For maximum security you should set minimum permissions, which is 640. The owner 6 would be the one uploading the files.


2 Answers

The mode is modified by your current umask, which is 022 in this case.

The way the umask works is a subtractive one. You take the initial permission given to mkdir and subtract the umask to get the actual permission:

  0777 - 0022 ======   0755 = rwxr-xr-x. 

If you don't want this to happen, you need to set your umask temporarily to zero so it has no effect. You can do this with the following snippet:

$oldmask = umask(0); mkdir("test", 0777); umask($oldmask); 

The first line changes the umask to zero while storing the previous one into $oldmask. The second line makes the directory using the desired permissions and (now irrelevant) umask. The third line restores the umask to what it was originally.

See the PHP doco for umask and mkdir for more details.

like image 148
paxdiablo Avatar answered Sep 25 '22 02:09

paxdiablo


The creation of files and directories is affected by the setting of umask. You can create files with a particular set of permissions by manipulating umask as follows :-

$old = umask(0); mkdir("test", 0777); umask($old); 
like image 32
Steve Weet Avatar answered Sep 23 '22 02:09

Steve Weet