chmod() 函数改变文件模式。chmod — changes file mode 如果成功则返回 true,否则返回 false。
语法
chmod(file,mode)
| 参数 | 描述 |
|---|---|
| file | 必需。规定要检查的文件。 |
| mode |
可选。规定新的权限。 mode 参数由 4 个数字组成:
可能的值(如需设置多个权限,请对下面的数字进行总计):
|
代码如下:
<?php
chmod("/somedir/somefile", 755); // 十进制数,可能不对
chmod("/somedir/somefile", "u+rwx,go+rx"); // 字符串,不对
chmod("/somedir/somefile", 0755); // 八进制数,正确的 mode 值
?>改进递归文件模式@ infosoft ....,这是一个小短,应处理的Linux文件系统的所有文件类型。这个可以批量更改文件或目录的权限
代码如下:
<?php
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;
}
?>如果你目录太多的话可以用
代码如下:
<?php
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $item) {
chmod($item, $filemode);
}
?>这段代码来修改目录的权限
以上就是php chmod()函数与批量修改文件目录权限的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号