PHP mkdir () No Write permission problem solving method _php instance

Source: Internet
Author: User
When you create a folder using MkDir, you find that the function has two parameters, and the second parameter specifies permissions for the newly created folder.

However, if you use mkdir directly (' File address ', 0777), you find that the permissions for the new folder are not 777, which is typically 022.

Because mkdir when setting permissions on a folder, it will be followed by the umask (user default permission attribute) value of the user who is currently logged on to the operating system, and the resulting value is the final permission value.

What is Umask?

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

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

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

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 permission.

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 (which is typically the default), that is: 000 010 010 is mkdir the specified 777, that is: 111 111 111 Bits "and" after the actual permissions are: 022.

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

1, modify user umask,php provided with umask function:

Copy the Code code as follows:
$oldumask =umask (0);
mkdir (' Test ', 0777);
Umask ($oldumask);

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

2, using the chmod function, which is also the most common method:
Copy the Code code as follows:
mkdir (' File Address ', 0777);
chmod (' File Address ', 0777);

Finally, it is important to note that permission values are best used in octal notation, which 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.