PHP chmod function and batch modification file directory Permissions _php Foundation

Source: Internet
Author: User
Tags chmod file permissions
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.

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.