语法
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。