How to change directory permissions in PHP

Posted on Tags

In general, there is no big problem and it is quite easy to change permissions of a directory to 0777 or something different, you simply do (if you create) mkdir('/project/data/videos', 0777) but this might not work…

If you check the permissions you will see that they are not 0777 but 0755 - drwxr-xr-x and this is because of your umask, which is 022 in this case.

So if we use 0777 as permission set, umask will be subtracted from our permissions.

0777
– 0022
======
0755

Because of umask we have to first get the current umask (you don’t want to change it forever), set it to zero, create the dir with 0777 and then return the default umask


$umask = umask(0);
mkdir('/project/data/videos', 0777);
umask($umask);

Now the permissions of the created directory will be 0777, if you want you can read more about umask here

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.