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

怪我咯
Release: 2023-03-13 12:52:02
Original
6694 people have browsed it

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

Syntax

chmod(file,mode)
Copy after login


##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 值 
?>
Copy after login

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; 
} 
?>
Copy after login

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); 
} 
?>
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!