PHP chmod function and bulk modify file directory permissions _php tutorial

Source: Internet
Author: User
Grammar
chmod (file,mode) parameter description
File required. Specifies the documents to be inspected.
mode is optional. Specify the new permissions.
The mode parameter consists of 4 numbers:
The first number is always 0.
The second number specifies the owner's permissions
The second number specifies the permissions of the user group to which the owner is a member
The fourth number stipulates the rights of all other persons
Possible values (if you want to set multiple permissions, make a total of the following numbers):
1-Execute Permissions
2-Write permissions
4-Read Permissions
Let's see a simple example.
Copy CodeThe code is as follows:
chmod ("/somedir/somefile", 755); Decimal number, may not be correct
chmod ("/somedir/somefile", "U+rwx,go+rx"); String, not
chmod ("/somedir/somefile", 0755); Octal number, correct mode value
?>

Improved recursive file mode @ Infosoft ...., this is a small short and should be handled by all file types of the Linux file system. This allows you to bulk change the permissions of a file or directory
Copy CodeThe code is as follows:
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;
}
?>

You can use it if you have too many catalogs.
Copy CodeThe code is as follows:
$iterator = new Recursiveiteratoriterator (new Recursivedirectoryiterator ($pathname), Recursiveiteratoriterator:: Self_first);
foreach ($iterator as $item) {
chmod ($item, $filemode);
}
?>

This code to modify the permissions of the directory
Haha, we don't just talk about chmod simple syntax, but also do a complex example of chmod use

Description
BOOL Chmod (string $filename, int $mode)
Attempts to change the schema of the file specified by filename to mode given.

Note that mode is not automatically treated as an octal value, and it cannot be used as a string (for example, "G+w"). To ensure proper operation, add 0 to the mode front:

The mode parameter contains three octal numbers in order to specify the owner, the group in which the owner resides, and the access restrictions for everyone. Each section can calculate the permissions you want by adding the required permissions. The number 1 means that the file is executable, the number 2 means that the file is writable, and the number 4 means that the file is readable. Add these numbers to make the required permissions. File permissions for UNIX systems can be read in the manual "Man 1 chmod" and "Man 2 chmod".
Copy CodeThe code is as follows:

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 ' s group
chmod ("/somedir/somefile", 0750);
?>

Returns TRUE if successful, and 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, and the files being inspected must be accessed through the server's file system.

Note: When Safe mode is turned on, PHP checks to see if the file being manipulated has the same UID (owner) as the script being executed. It is important to note that suid,sgid and sticky bits cannot be modified.

http://www.bkjia.com/PHPjc/321788.html www.bkjia.com true http://www.bkjia.com/PHPjc/321788.html techarticle The syntax chmod (file,mode) parameter describes the file required. Specifies the documents to be inspected. mode is optional. Specify the new permissions. The mode parameter consists of 4 numbers: The first number is always 0 second ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.