Home >Backend Development >PHP Tutorial >PHP chmod function and batch modification of file directory permissions_PHP tutorial

PHP chmod function and batch modification of file directory permissions_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:171196browse

Syntax
chmod(file,mode) Parameter Description
file Required. Specifies the documents to be checked.
mode is 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 permissions for everyone else
Possible values ​​(to set multiple permissions, total the numbers below):
1 - Execute permissions
2 - Write permissions
4 - Read permissions
Let’s look at a simple example

Copy the code The code is as follows:

chmod("/somedir/somefile", 755); // Decimal number, may be wrong
chmod("/somedir/somefile", "u+rwx,go+rx"); // String , incorrect
chmod("/somedir/somefile", 0755); // Octal number, correct mode value
?>

Improved recursive file mode @ infosoft ... ., which is a little short and should handle all file types of Linux file systems. This can batch change the permissions of files or directories
Copy code The 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;
}
?>

If you have too many directories, you can use
Copy code The code is as follows:

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

This code comes Modify the permissions of the directory
Haha, we not only talked about the simple syntax of chmod, but also made complicated instructions about chmod usage examples


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 it cannot be a string (such as "g+w"). To ensure correct operation, you need to add 0 in front of the 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".
Copy code The 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, 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321788.htmlTechArticleSyntax chmod(file,mode) Parameter Description file Required. Specifies the documents to be checked. mode is optional. Specify new permissions. The mode parameter consists of 4 numbers: the first number is always 0 the second...
Statement:
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