Detailed explanation of PHP's file permission function chmod

*文
Release: 2023-03-19 09:06:01
Original
5895 people have browsed it

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

Syntax
chmod(file,mode) Parameter Description
file Required. Specifies the documents to be checked.
mode Optional. Specify new permissions.
The mode parameter consists of 4 numbers:
The first number is always 0
The second number specifies the permissions of the owner
The second number specifies the permissions of the user group to which the owner belongs
The fourth number specifies the permissions of everyone else
Possible values ​​(to set multiple permissions, please total the numbers below):
1 - Execute permissions
2 - Write permissions
4 - Read permissions
Let’s see a simple example

<?php 
chmod("/somedir/somefile", 755); // 十进制数,可能不对 
chmod("/somedir/somefile", "u+rwx,go+rx"); // 字符串,不对 
chmod("/somedir/somefile", 0755); // 八进制数,正确的 mode 值 
?>
Copy after login

Improved recursive file mode@infosoft...., This is a small shortcut that should handle all file types of the Linux file system. This can change the permissions of files or directories in batches

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

<?php 
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST); 
foreach($iterator as $item) { 
chmod($item, $filemode); 
} 
?>
Copy after login

This code to modify the permissions of the directory
Haha, we are not just talking about simple chmod syntax, and also made complex examples of chmod usage
Explanation
bool chmod (string $filename, int $mode)
Try to change the mode of the file specified by filename to that given by mode.
Note that mode will not be automatically treated as an octal value, and strings (such as "g+w") cannot be used. To ensure correct operation, you need to add 0 in front of mode:
The mode parameter contains three octal numbers that specify the owner, the owner's group, and everyone's access restrictions in order. Each part can be calculated by adding the required permissions. The number 1 makes the file executable, the number 2 makes the file writable, and the number 4 makes the file readable. Add these numbers to specify the required permissions. For information about file permissions on UNIX systems, read the manuals "man 1 chmod" and "man 2 chmod".

<?php 
// Read and write for owner, nothing for everybody else 
chmod("/somedir/somefile", 0600); 
// Read and write for owner, read for everybody else 
chmod("/somedir/somefile", 0644); 
// Everything for owner, read and execute for others 
chmod("/somedir/somefile", 0755); 
// Everything for owner, read and execute for owner&#39;s group 
chmod("/somedir/somefile", 0750); 
?>
Copy after login

Returns TRUE if successful and FALSE if failed.
Note: The current user refers to the user executing PHP. Most likely not the same as the usual shell or FTP user. On most systems a file's mode can only be changed by the user who owns the file.
Note: This function cannot be used on remote files. The files being checked must be accessed through the server's file system.
Note: When safe mode is turned on, PHP will check whether the file being operated on has the same UID (owner) as the script being executed. It should be noted that SUID, SGID and sticky bits cannot be modified.

The above is the detailed content of Detailed explanation of PHP's file permission function chmod. 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