Analysis on permissions of mkdir () function in php, phpmkdir
Problem description:
Use the following php code to create a directory. The expected permission for the directory is 0777, and the actual result is 0755.
Mkdir ('./aa/', 0777 );
Analysis and Test results:
1. The directory permission specified by the mkdir () function must be less than or equal to the default permission set by the system umask.
For example, the default umask in linux is generally 0022, that is, the default permission for creating a directory is 0755. Therefore, the directory permission obtained by php mkdir ('./aa/', 0777) is 0755.
Xw @ xw-X201 :~ /Desktop/dd/aa $ umask0022xw @ 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 usage 20 drwxrwxrwx 5 xw 4096. /drwxr-xr-x 6 xw 4096 .. /drwxr-xr-x 2 xw 4096 2012-06-01 xw/drwxr-xr-x 2 xw 4096 2012-06-01 xw1/drwxr -- r-x 2 xw 4096 2012-06-01 xw2/
Change the default system permission to 777, and mkdir () can specify more permissions.
Xw @ xw-X201 :~ /Desktop/dd/aa $ umask 000xw @ xw-X201 :~ /Desktop/dd/aa $ php-r "mkdir ('./xw2/', 0777);" xw @ xw-X201 :~ /Desktop/dd/aa $ ll total usage 12 drwxrwxrwx 3 xw 4096. /drwxr-xr-x 6 xw 4096 .. /drwxrwxrwx 2 xw 4096 xw2/
2. the chmod () function is not affected by the system umask. You can set a higher permission than the default permission of the system.
Xw @ xw-X201 :~ /Desktop/dd/aa $ umask0022xw @ 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 usage 20 drwxrwxrwx 5 xw 4096. /drwxr-xr-x 6 xw 4096 .. /drwxr-xr-x 2 xw 4096 2012-06-01 xw1/drwxr-xr-x 2 xw 4096 2012-06-01 xw2/drwxrwxrwx 2 xw 4096 2012-06-01 xw3/
3. The mkdir command of the system can be used to set permissions greater than the default value.
Xw @ xw-X201 :~ /Desktop/dd/aa $ umask0022xw @ xw-X201 :~ /Desktop/dd/aa $ mkdir-m0777 xwxw @ xw-X201 :~ /Desktop/dd/aa $ ll total usage 12 drwxrwxrwx 3 xw 4096. /drwxr-xr-x 6 xw 4096 .. /drwxrwxrwx 2 xw 4096 xw/
Summary:
To create a directory with read and write permissions for php, you 'd better use the following code. Do not directly use the mkdir function to specify permissions to avoid the impact of the umask system.
mkdir('./xw/');chmod('./xw/',0777);