Home >Backend Development >PHP Tutorial >php chmod() function and batch modification of file directory permissions

php chmod() function and batch modification of file directory permissions

怪我咯
怪我咯Original
2017-07-10 16:25:186822browse

chmod() function changes the file mode. chmod — Changes file mode Returns TRUE if successful, FALSE otherwise.

Syntax

chmod(file,mode)


##ParametersDescriptionfileRequired. Specifies the documents to be checked. mode
Optional. Specify new permissions.

mode parameter consists of 4 numbers:

  • The first number is always 0

  • The second number is specified The owner's permissions

  • The second number specifies the permissions of the user group to which the owner belongs

  • The fourth number specifies the permissions of everyone else Permissions

Possible values ​​(to set multiple permissions, total the numbers below):

  • 1 - Execute permissions

  • 2 - Write permission

  • 4 - Read permission


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

The code is as follows:

<?php 
function chmodr($path, $filemode) { 
if (!is_dir($path)) 
return chmod($path, $filemode); 
$dh = opendir($path); 
while (($file = readdir($dh)) !== false) { 
if($file != &#39;.&#39; && $file != &#39;..&#39;) { 
$fullpath = $path.&#39;/&#39;.$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


The code is as follows:

<?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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn