Grammar
chmod (file,mode) parameter description
File required. Specify the files to be inspected.
mode is optional. Specify the new permissions.
The mode parameter consists of 4 digits:
The first number is always 0.
The second number stipulates the permissions of the owner
The second number stipulates the permissions of the user group to which the owner belongs
Number fourth stipulates the rights of all other persons
Possible values (for example, if you want to set multiple permissions, total the following numbers):
1-Execute Permissions
2-Write permission
4-Read permission
Take a look at a simple example
Copy Code code as follows:
<?php
chmod ("/somedir/somefile", 755); Decimal number, may not be correct
chmod ("/somedir/somefile", "U+rwx,go+rx"); String, no
chmod ("/somedir/somefile", 0755); Octal number, correct mode value
?>
Improved recursive file mode @ Infosoft ..., which is a little short, should be handled by all file types of Linux file system. This allows you to bulk change permissions for a file or directory
Copy Code code 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!= '. ' && $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 catalogues, you can use
Copy Code code as follows:
<?php
$iterator = new Recursiveiteratoriterator new Recursivedirectoryiterator ($pathname), Recursiveiteratoriterator:: Self_first);
foreach ($iterator as $item) {
chmod ($item, $filemode);
}
?>
This code to modify permissions on the directory
Haha, we don't just talk about chmod simple syntax, but also do a complex example of chmod usage
Description
BOOL Chmod (string $filename, int $mode)
Attempts to change the schema of the file specified by filename to the given in mode.
Note that mode is not automatically treated as a octal value and cannot be used with strings (such as "g+w"). To ensure proper operation, you need to precede mode with 0:
The mode parameter contains three octal numbers that specify the owner, the group of owners, and the access restrictions for everyone in order. Each section can calculate the permissions that you want by adding the required permissions. The number 1 indicates that the file is executable, the number 2 indicates that the file is writable, and the number 4 indicates that the file is readable. Add these numbers to set the required permissions. File permissions on UNIX systems can read the manual "Man 1 chmod" and "Man 2 chmod".
Copy Code code as follows:
<?php
Read and write for owner, no 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 ' s group
chmod ("/somedir/somefile", 0750);
?>
Returns TRUE if successful, and returns FALSE if it fails.
Note: The current user refers to the user who executes PHP. It is probably not the same as the usual shell or FTP user. In most systems the file mode can only be changed by the user of the file owner.
Note: This function does not work on remote files, the files being checked must be accessed through the server's file system.
Note: When Safe mode is open, PHP checks to see if the file being manipulated has the same UID (owner) as the script being executed. Note that you cannot modify suid,sgid and sticky bits.