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
You can use chmod() to do this.
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).
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With