Home >Backend Development >PHP Tutorial >php chmod() function and batch modification of file directory permissions
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 值 ?>Improve recursive file mode @ infosoft...., this is a small short that should be dealt with All file types of Linux
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; } ?>If you have too many directories, you can use
<?php $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST); foreach($iterator as $item) { chmod($item, $filemode); } ?>This code is used to modify the permissions of the directory
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!