PHP Mkdir Permission Does Not Work


PHP provides a function called "mkdir" that lets you create a new directory. An optional argument specifies the numeric value of the permissions to be assigned to the new directory. For example, permissions of "0777" provide read, write and execute permissions for the "owner," "group" and "other." There are several instances in which permission assignment with the "mkdir" function will appear as though it doesn't work because the new directory does not have the same permissions you passed to the function.

Not Applicable to Windows

  • The list of permissions you supply to the "mkdir" function are valid only on Linux-based file systems. This means you cannot associate permissions with a directory you create under Windows. If you pass permissions with the "mkdir" function under Windows, PHP simply ignores them.

Default User Account

  • Remember that a script run on a Web server from a browser runs under the default user account on the Web server such as "www-data," not under your user account. If directory permissions are not working, it may be because you are testing a script under your account instead of the default user account. You can test the script as the default user by typing "su www-data" and exploring the directory permissions assigned to that user.

Directory Mask

  • If the resulting permissions of a new directory are different from what you passed, it may be because of a directory mask applied to the permissions. The mask is maintained by the Web server, not by PHP. To circumvent the mask, use the "umask" function to set the mask to zero, create the directory using "mkdir" and change the directory permissions using the "chmod" function.

    For example:

    $save = umask(0);
    if (mkdir($newdir)) chmod($newdir, 0755);

Recursive Assignment

  • The "mkdir" function takes a second optional argument that creates nested directories recursively. When you assign directory permissions recursively, the permissions may not be executed in the proper order and you might get different results from what you expected. To ensure the proper order, use the "chmod" function to assign directory permissions to the new directory after you create it with the "mkdir" function.

    For example:

    if (mkdir($newdir, 0755, true)) chmod($newdir, 0755);

Related Searches


Promoted By Zergnet



Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!