Permission problem for mkdir () function in PHP (GO)

Source: Internet
Author: User

Problem Description:

Use the following PHP code to create a directory, the permissions of the expected directory is 0777, the actual result is 0755

[PHP]View Plaincopy
    1. mkdir ('./aa/', 0777);

Analysis and test results:

The 1.mkdir () function specifies that the directory permission can be less than or equal to the default permissions set by the system umask.

such as the Linux default Umask general 0022, that is, the default permission to create a directory is 0755, so php mkdir ('./aa/', 0777) the permission to get the directory is 0755.

[Plain]View Plaincopy
  1. [Email protected]:~/desktop/dd/aa$ umask
  2. 0022
  3. [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw/', 0777);"
  4. [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw1/', 0755);"
  5. [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw2/', 0747);"
  6. [Email protected]:~/desktop/dd/aa$ ll
  7. Total dosage 20
  8. DRWXRWXRWX 5 XW xw 4096 2012-06-01 11:58./
  9. Drwxr-xr-x 6 XW xw 4096 2012-06-01 11:26.. /
  10. Drwxr-xr-x 2 XW xw 4096 2012-06-01 11:57 xw/
  11. Drwxr-xr-x 2 XW xw 4096 2012-06-01 11:58 xw1/
  12. Drwxr--r-x 2 XW xw 4096 2012-06-01 11:58 xw2/

Change the system default permission to 777,mkdir () to specify a larger permission

[HTML]View Plaincopy
    1. [Email protected]:~/desktop/dd/aa$ Umask 000
    2. [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw2/', 0777);"
    3. [Email protected]:~/desktop/dd/aa$ ll
    4. Total Dosage 12
    5. DRWXRWXRWX 3 XW xw 4096 2012-06-01 12:08./
    6. Drwxr-xr-x 6 XW xw 4096 2012-06-01 11:26.. /
    7. DRWXRWXRWX 2 XW xw 4096 2012-06-01 12:08 xw2/

The 2.chmod () function is not affected by the system umask and can be set to a greater extent than the system's default permissions.

[Plain]View Plaincopy
  1. [Email protected]:~/desktop/dd/aa$ umask
  2. 0022
  3. [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw1/', 0777);"
  4. [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw2/');"
  5. [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw3/'); chmod ('./xw3/', 0777);"
  6. [Email protected]:~/desktop/dd/aa$ ll
  7. Total dosage 20
  8. DRWXRWXRWX 5 XW xw 4096 2012-06-01 12:12./
  9. Drwxr-xr-x 6 XW xw 4096 2012-06-01 11:26.. /
  10. Drwxr-xr-x 2 XW xw 4096 2012-06-01 12:11 xw1/
  11. Drwxr-xr-x 2 XW xw 4096 2012-06-01 12:11 xw2/
  12. DRWXRWXRWX 2 XW xw 4096 2012-06-01 12:12 xw3/


3. The system's mkdir command can set permissions greater than the default value.

[Plain]View Plaincopy
    1. [Email protected]:~/desktop/dd/aa$ umask
    2. 0022
    3. [Email protected]:~/desktop/dd/aa$ mkdir-m0777 XW
    4. [Email protected]:~/desktop/dd/aa$ ll
    5. Total Dosage 12
    6. DRWXRWXRWX 3 XW xw 4096 2012-06-01 12:18./
    7. Drwxr-xr-x 6 XW xw 4096 2012-06-01 11:26.. /
    8. DRWXRWXRWX 2 XW xw 4096 2012-06-01 12:18 xw/

Summarize:

PHP to create a directory with read and write permissions, it is best to use the following code, do not use the MkDir function directly to specify permissions to avoid the effects of system umask

[PHP]View Plaincopy
    1. mkdir ('./xw/');
    2. chmod ('./xw/', 0777);



Reference article:

LINUX Umask Detailed

PHP using mkdir () to create new directory without write permission issues

Original: http://blog.csdn.net/tsxw24/article/details/7622832

Permission problem for mkdir () function in PHP (GO)

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.