Solution to the problem of PHP mkdir of without write permission

  • 2021-07-01 06:51:50
  • OfStack

When you create a folder using mkdir, you find that this function has two parameters. The second parameter is to specify permissions for the newly created folder.

But if you use mkdir directly ('File Address', 0777); When you find that the permission of the new folder is not 777, it will be 022 in general.

Because when mkdir sets permissions for folders, it will make a bit "AND" with the umask (user default permission attribute) value of the currently logged-in operating system user, and the obtained value is the final permission value.

What is umask?

How do we get the default permission to create files? How can I change this default permission?

When we log in to the system, we always have a default permission to create a file, so how does this permission come from? This is what umask does.

umask sets the default permission for users to create files, which is just the opposite of chmod. umask sets the permission "complement", while chmod sets the file permission code. 1 Sets the umask value in /etc/profile, $HOME/.bash_profile, or $HOME/.profile.

How do I calculate the umask value?

The umask command allows you to set the default mode for file creation, and there is one number in the corresponding umask value for each class of users (file owner, same group of users and other users). For files, the maximum values of these 1 numbers are 6 respectively. The system does not allow you to create a text file on its execution permission, must be created after the chmod command to increase this 1 permission. Directories allow execution permissions to be set, so that each number in umask can be up to 7 for directories.

The general form of this command is umask nnn, where nnn can be 000-777.

We just need to remember that umask "takes" the corresponding bit from the permission.

If the umask value is 022, the default directory permission is 755 and the default file permission is 644.
Therefore, if the user umask is 022 (1 default is this), that is,: 000 010 010 lies in the 777 specified by mkdir, that is,: 111 111 111 bits "and", the real permission obtained is: 022.

If you want to maximize the permissions of a new folder, there are two ways to do it: (if the current user can give the highest permissions, of course)

1. Modify the user umask. php provides umask function:


$oldumask=umask(0);
mkdir('test',0777);
umask($oldumask);

This method seems to be easy. Specify the value of umask in the beginning file of the script, and then use mkdir directly to control the authority. It should be noted that when using umask function on a multithreaded server, multiple threads will share one umask, which may cause confusion.

2. Use the chmod function, which is also the most commonly used method:


mkdir(' File address ', 0777);
chmod(' File address ', 0777);

Finally, it is important to note that the permission value is best expressed in octal system, that is, "0" begins, and 1 must not be quoted.


Related articles: