PHP chmod function with batch modify file directory permissions

  • 2020-03-31 20:39:15
  • OfStack

grammar
Chmod (file,mode) parameter description
The file required. The document specified for inspection.
Mode optional. Set new limits.
The mode parameter consists of four digits:
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 defines the authority of all others
Possible values (if you need to set multiple permissions, please sum up the following Numbers) :
1 - execute permission
2 - write permission
4 - read permissions
Let's do a simple example
 
<?php 
chmod("/somedir/somefile", 755); //Decimal number, probably not right
chmod("/somedir/somefile", "u+rwx,go+rx"); //String, no
chmod("/somedir/somefile", 0755); //Octal number, the correct mode value
?> 

Improved recursive file mode @infosoft... This is a small short file that should handle all file types of the Linux file system. This allows you to change the permissions of a file or directory in bulk
 
<?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; 
} 
?> 

You can use it if you have too many catalogues
 
<?php 
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST); 
foreach($iterator as $item) { 
chmod($item, $filemode); 
} 
?> 

This code modifies the permissions of the directory
Haha, we not only talked about chmod simple syntax, but also made complex use cases of chmod

instructions
Bool chmod (string $filename, int $mode)
Attempts to change the pattern of the file specified by filename to that given by mode.

Note that mode is not automatically treated as an octal value and cannot use strings (for example "g+w"). To ensure proper operation, the mode needs to be preceded by 0:

The mode parameter contains three octal Numbers that specify, in order, the access restrictions for the owner, the group the owner belongs to, and the owner. Each part 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 specify the required permissions. File permissions for UNIX systems are available in the "man 1 chmod" and "man 2 chmod" manuals.
 

<?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); 
?> 

Returns TRUE on success and FALSE on failure.

Note: the current user is the user executing PHP. Probably not the same as a typical shell or FTP user. On 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 checked files must be accessed through the server's file system.

Note: when security mode is turned on, 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.

Related articles: