chmod() function changes the file mode. chmod — Changes file mode Returns TRUE if successful, FALSE otherwise.
Syntax
chmod(file,mode)
Description | |
---|---|
Required. Specifies the documents to be checked. | |
Optional. Specify new permissions. mode parameter consists of 4 numbers:
|
The code is as follows:
<?php chmod("/somedir/somefile", 755); // 十进制数,可能不对 chmod("/somedir/somefile", "u+rwx,go+rx"); // 字符串,不对 chmod("/somedir/somefile", 0755); // 八进制数,正确的 mode 值 ?>
File System. This can batch change the permissions of files or directories
<?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); } ?>
The above is the detailed content of php chmod() function and batch modification of file directory permissions. For more information, please follow other related articles on the PHP Chinese website!