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

sachin
edited May 2022 in PHP

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

Tagged:

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.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!