php chmod设置目录权限设置,PHP chmod 函数与批量修改文件目录权限 | 六阿哥博客...

语法

bool chmod ( string $filename , int $mode )

1

boolchmod(string$filename,int$mode)

参数描述

filename:规定要检查的文件。

mode :规定新的权限,由 4 个数字组成。

第一个数字永远是 0

第二个数字规定所有者的权限

第二个数字规定所有者所属的用户组的权限

第四个数字规定其他所有人的权限

权限对应数字:

1 - 执行权限

2 - 写权限

4 - 读权限

如果成功则返回 TRUE,失败则返回 FALSE。

注意 mode 不会被自动当成八进制数值,而且也不能用字符串(例如 "g+w")。要确保正确操作,需要给 mode 前面加上 0。

下面来看一个简单的例子:

chmod("/somedir/somefile", 755); // 十进制数,可能不对

chmod("/somedir/somefile", "u+rwx,go+rx"); // 字符串,不对

chmod("/somedir/somefile", 0755); // 八进制数,正确的 mode 值

?>

1

2

3

4

5

chmod("/somedir/somefile",755);// 十进制数,可能不对

chmod("/somedir/somefile","u+rwx,go+rx");// 字符串,不对

chmod("/somedir/somefile",0755);// 八进制数,正确的 mode 值

?>

改进递归文件模式@ infosoft ....,这是一个小短,应处理的Linux文件系统的所有文件类型。这个可以批量更改文件或目录的权限。

function chmodr($path, $filemode) {

if (!is_dir($path))

return chmod($path, $filemode);

$dh = opendir($path);

while (($file = readdir($dh)) !== false) {

if($file != '.' && $file != '..') {

$fullpath = $path.'/'.$file;

if(is_link($fullpath))

return FALSE;

elseif(!is_dir($fullpath) && !chmod($fullpath, $filemode))

return FALSE;

elseif(!chmodr($fullpath, $filemode))

return FALSE;

}

}

closedir($dh);

if(chmod($path, $filemode))

return TRUE;

else

return FALSE;

}

?>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

functionchmodr($path,$filemode){

if(!is_dir($path))

returnchmod($path,$filemode);

$dh=opendir($path);

while(($file=readdir($dh))!==false){

if($file!='.'&&$file!='..'){

$fullpath=$path.'/'.$file;

if(is_link($fullpath))

returnFALSE;

elseif(!is_dir($fullpath)&&!chmod($fullpath,$filemode))

returnFALSE;

elseif(!chmodr($fullpath,$filemode))

returnFALSE;

}

}

closedir($dh);

if(chmod($path,$filemode))

returnTRUE;

else

returnFALSE;

}

?>

如果你目录太多的话可以用

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $item) {

chmod($item, $filemode);

}

?>

1

2

3

4

5

6

$iterator=newRecursiveIteratorIterator(newRecursiveDirectoryIterator($pathname),RecursiveIteratorIterator::SELF_FIRST);

foreach($iteratoras$item){

chmod($item,$filemode);

}

?>

Note: 当前用户指的是执行 PHP 的用户,很可能和通常的 shell 或者 FTP 用户不是同一个,在大多数系统下文件模式只能被文件所有者的用户改变。

Note: 本函数不能作用于远程文件,被检查的文件必须通过服务器的文件系统访问。

Note: 当安全模式打开的时候,PHP 会检查所操作的文件是否和正在执行的脚本具有相同的 UID (所有者)。要注意的是,不能修改 SUID,SGID 和 sticky bits。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值