PHP mkdir () problem-solving method with no write permission _php instance

Source: Internet
Author: User
Tags chmod mkdir

When you create a folder using MkDir, you find that the function has two parameters, and the second parameter is to assign permissions to the newly created folder.

However, if you use MkDir (' File Address ', 0777), you will find that the permissions for the new folder are not 777, and typically 022.

Because mkdir gives a folder permission, it will be bit "and" with the value of the umask (user default permission attribute) of the current logged-on operating system user, and the resulting value is the final permission value.

What is Umask?

How do we get the default permissions for creating files? How do you change this default permission?

When we log on to the system and create a file that always has a default permission, how does this permission come about? That's what Umask did.

Umask set the user to create the default permissions of the file, it and chmod effect just the opposite, Umask set is the permission "complement", and chmod set is the file permission code. Umask values are generally set 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 at the time the file was created, and there is a number in the corresponding Umask value for each type of user (file owner, same group user, other user). For a file, the maximum value for this number is 6. The system does not allow you to give permission to execute a text file when it is created, and you must use the chmod command to add this permission after creation. The directory allows you to set execution permissions so that the number of umask can be up to 7 for the directory.

The general form of the command is: Umask nnn, where nnn can be 000-777.

We just have to remember that Umask is "taking" the appropriate bits from the right.

For example: The Umask value is 022, the default directory permission is 755, and the default file permission is 644.
So, if the user umask is 022 (generally by default is this), that is: 000 010 010 is mkdir specified 777, that is: 111 111 "and" after the real permission is: 111.

If you want to create a new folder with the most permissions, there are two ways to do it: (Of course, under the condition that the current user can give the highest privileges)

1, modify the user umask,php provide a umask function:

Copy Code code as follows:

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

This method seems to be once and for all, in the script in the beginning of the file to specify the next Umask value, the following directly with the mkdir can control permissions, it should be noted that when using the Umask function on a multithreaded server, multiple threads will be common to a umask, so it may cause confusion.

2, use the chmod function, this is also the most common method:

Copy Code code as follows:

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

Finally, it should be noted that the privilege value is best used octal, that is, "0", and must not be quoted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.