Why PHP script does not create a directory with 777 permissions ?

In PHP, we aren’t able to create a directory with 777 permission because of default umask. The umask is called a user mask or user file creation mask. This is a kind of base permission or default permission given when a new file or folder is created. The default value of umask is generally 022. The umask sets in which permissions must be removed from the system default when you create a file or a directory.

First of all, let’s understand how umask actually works during file or directory creation and permission setup. We will use mkdir() function in below examples.

It is important to understand that umask revokes, deletes permissions from system default, so it can’t grant permissions the system default hasn’t. In the above example, with the 666 system default, there is no way you can use umask to create a file with execute permission.

So, When we try to create a directory with permission 777, instead of getting drwxrwxrwx we get drwxr-xr-x which corresponds to 755 ( subtracting 022 from 777). This is the reason why we aren’t able to create a directory with permission 777.

It’s not over yet, as I never said it is impossible to create a directory with permission 777. So let’s learn how to create a directory with 777 permission, follow the below steps:

This is how you can create a directory with 777 permission. The permission 777 in symbolic representation for directory is drwxrwxrwx. This permission allows the owner user, group, and others to read, write, and execute, so it has no restriction. This permission means that anyone who is a user on the same server can read, write, and execute the file. In the case of a folder, anyone who is a user will be able to copy files to it. This obviously sounds dangerous if you are using a shared server for your website.

References: https://www.php.net/manual/en/function.umask.php


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :