SoFunction
Updated on 2025-03-04

PHP chmod function and batch modification file directory permissions

grammar
chmod(file,mode) parameter description
file Required. Specify documents to be inspected.
mode is optional. Specify 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 belongs
The fourth number stipulates the authority of everyone else
Possible values ​​(If you need to set multiple permissions, total the following numbers):
1 - Execution permissions
2 - Write permission
4 - Read permissions
Let's take a look at a simple example
Copy the codeThe code is as follows:

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

Improved recursive file mode @ infosoft..., this is a small short, should handle all file types of Linux file systems. This allows you to batch change permissions for files or directories
Copy the codeThe code is 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 directories, you can use it
Copy the codeThe code is as follows:

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

This code changes the permissions of the directory
Haha, we not only talk about the simple syntax of chmod, but also made complicated examples of using chmod

illustrate
bool chmod ( string $filename , int $mode )
Try changing the pattern of the file specified by filename to the given by mode.

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 correct operation, you need to prefix the mode:

The mode parameter contains three octal numbers that specify the owner, the group where the owner is located, and the access restrictions for everyone. Each section can calculate the required permissions by adding the required permissions. The number 1 means making the file executable, the number 2 means making the file writable, and the number 4 means making the file readable. Add these numbers to define the required permissions. For file permissions for UNIX systems, you can read the manuals "man 1 chmod" and "man 2 chmod".
Copy the codeThe code is as follows:


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

Return TRUE if successful, and FALSE if failed.

Note: The current user refers to the user who executes PHP. It is likely not the same as the usual shell or FTP user. In most systems, file mode can only be changed by the user of the file owner.


Note: This function cannot act on remote files, and the files being checked must be accessed through the server's file system.

Note: When Safe Mode is turned on, PHP checks whether the file being operated has the same UID (Owner) as the script being executed. It should be noted that SUID, SGID and sticky bits cannot be modified.