php的mkdir()函数创建文件夹比较安全的权限设置方法_php实例

WBOY
Release: 2016-06-07 17:17:07
Original
958 people have browsed it

问题描述:
使用以下php代码创建了一个目录,期望目录的权限是0777,实际结果是0755

复制代码 代码如下:
mkdir('./aa/',0777);

分析与测试结果:

 1.mkdir()函数指定的目录权限只能小于等于系统umask设定的默认权限。

如linux默认的umask一般0022, 即创建目录的默认权限是0755, 所以这时php  mkdir('./aa/',0777) 得到目录的权限是0755.

复制代码 代码如下:

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
总用量 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/

把系统默认权限改为777,mkdir()可以指定更大权限了
复制代码 代码如下:

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
总用量 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/

2.chmod()函数不受系统umask影响,可以设置比系统默认权限大的权限。

复制代码 代码如下:

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
总用量 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.系统的mkdir命令可以设置大于默认值的权限。

复制代码 代码如下:

xw@xw-X201:~/Desktop/dd/aa$ umask
0022
xw@xw-X201:~/Desktop/dd/aa$ mkdir -m0777 xw
xw@xw-X201:~/Desktop/dd/aa$ ll
总用量 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/

  

总结:

php若要创建一个有读写权限的目录,最好使用以下代码,不要直接使用mkdir函数指定权限,以避免系统umask的影响

复制代码 代码如下:

mkdir('./xw/');
chmod('./xw/',0777);
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!