PHP mkdir () function to create a folder a more Secure permission setting method _php Instance

Source: Internet
Author: User
Tags chmod mkdir

Problem Description:
Use the following PHP code to create a directory that expects directory permissions to be 0777 and the actual result is 0755

Copy Code code as follows:
mkdir ('./aa/', 0777);

Analysis and test results:

The directory permissions specified by the 1.mkdir () function can only be less than the default permissions set by the system umask.

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

Copy Code code as follows:

xw@xw-x201:~/desktop/dd/aa$ Umask
0022
xw@xw-x201:~/desktop/dd/aa$ php-r "mkdir ('./xw/', 0777);"
xw@xw-x201:~/desktop/dd/aa$ php-r "mkdir ('./xw1/', 0755);"
xw@xw-x201:~/desktop/dd/aa$ php-r "mkdir ('./xw2/', 0747);"
xw@xw-x201:~/desktop/dd/aa$ LL
Total dosage 20
DRWXRWXRWX 5 XW xw 4096 2012-06-01 11:58./
Drwxr-xr-x 6 XW xw 4096 2012-06-01 11:26. /
Drwxr-xr-x 2 XW XW 4096 11:57
Drwxr-xr-x 2 XW XW 4096 11:58
Drwxr--r-x 2 XW XW 4096 11:58

Change the system default permissions to 777,mkdir () to specify greater permissions
Copy Code code as follows:

Xw@xw-x201:~/desktop/dd/aa$ Umask 000
xw@xw-x201:~/desktop/dd/aa$ php-r "mkdir ('./xw2/', 0777);"
xw@xw-x201:~/desktop/dd/aa$ LL
Total Dosage 12
DRWXRWXRWX 3 XW xw 4096 2012-06-01 12:08./
Drwxr-xr-x 6 XW xw 4096 2012-06-01 11:26. /
DRWXRWXRWX 2 XW XW 4096 12:08

The 2.chmod () function is not affected by the system Umask and can set permissions larger than the system default permissions.

Copy Code code as follows:

xw@xw-x201:~/desktop/dd/aa$ Umask
0022
xw@xw-x201:~/desktop/dd/aa$ php-r "mkdir ('./xw1/', 0777);"
xw@xw-x201:~/desktop/dd/aa$ php-r "mkdir ('./xw2/');"
xw@xw-x201:~/desktop/dd/aa$ php-r "mkdir ('./xw3/'); chmod ('./xw3/', 0777);"
xw@xw-x201:~/desktop/dd/aa$ LL
Total dosage 20
DRWXRWXRWX 5 XW xw 4096 2012-06-01 12:12./
Drwxr-xr-x 6 XW xw 4096 2012-06-01 11:26. /
Drwxr-xr-x 2 XW XW 4096 12:11
Drwxr-xr-x 2 XW XW 4096 12:11
DRWXRWXRWX 2 XW XW 4096 12:12

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

Copy Code code as follows:

xw@xw-x201:~/desktop/dd/aa$ Umask
0022
xw@xw-x201:~/desktop/dd/aa$ mkdir-m0777 XW
xw@xw-x201:~/desktop/dd/aa$ LL
Total Dosage 12
DRWXRWXRWX 3 XW xw 4096 2012-06-01 12:18./
Drwxr-xr-x 6 XW xw 4096 2012-06-01 11:26. /
DRWXRWXRWX 2 XW XW 4096 12:18

  

Summarize:

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

Copy Code code as follows:

mkdir ('./xw/');
chmod ('./xw/', 0777);

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.