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
- 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
- [Email protected]:~/desktop/dd/aa$ umask
- 0022
- [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw/', 0777);"
- [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw1/', 0755);"
- [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw2/', 0747);"
- [Email protected]:~/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 2012-06-01 11:57 xw/
- Drwxr-xr-x 2 XW xw 4096 2012-06-01 11:58 xw1/
- 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
- [Email protected]:~/desktop/dd/aa$ Umask 000
- [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw2/', 0777);"
- [Email protected]:~/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 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
- [Email protected]:~/desktop/dd/aa$ umask
- 0022
- [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw1/', 0777);"
- [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw2/');"
- [Email protected]:~/desktop/dd/aa$ php-r "mkdir ('./xw3/'); chmod ('./xw3/', 0777);"
- [Email protected]:~/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 2012-06-01 12:11 xw1/
- Drwxr-xr-x 2 XW xw 4096 2012-06-01 12:11 xw2/
- 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
- [Email protected]:~/desktop/dd/aa$ umask
- 0022
- [Email protected]:~/desktop/dd/aa$ mkdir-m0777 XW
- [Email protected]:~/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 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
- mkdir ('./xw/');
- 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)