Home > Backend Development > PHP Tutorial > Detailed explanation of how to recursively execute the chmod command on a folder in PHP

Detailed explanation of how to recursively execute the chmod command on a folder in PHP

怪我咯
Release: 2023-03-12 18:08:02
Original
1527 people have browsed it

This article mainly introduces the method of PHP executing the chmod command on the folder recursively, which can realize the function of recursively executing the chmod command to change the folder execution permissions. Friends who need it can refer to it

The example in this article describes how PHP recursively executes the chmod command on a folder. Share it with everyone for your reference. The specific analysis is as follows:

Here recursively execute the chmod command on folders and files to change execution permissions

<?php
  function recursiveChmod($path, $filePerm=0644, $dirPerm=0755)
  {
   // Check if the path exists
   if(!file_exists($path))
   {
     return(FALSE);
   }
   // See whether this is a file
   if(is_file($path))
   {
     // Chmod the file with our given filepermissions
     chmod($path, $filePerm);
   // If this is a directory...
   } elseif(is_dir($path)) {
     // Then get an array of the contents
     $foldersAndFiles = scandir($path);
     // Remove "." and ".." from the list
     $entries = array_slice($foldersAndFiles, 2);
     // Parse every result...
     foreach($entries as $entry)
     {
      // And call this function again recursively, with the same permissions
      recursiveChmod($path."/".$entry, $filePerm, $dirPerm);
     }
     // When we are done with the contents of the directory, we chmod the directory itself
     chmod($path, $dirPerm);
   }
   // Everything seemed to work out well, return TRUE
   return(TRUE);
  }
?>
Copy after login

The above is the detailed content of Detailed explanation of how to recursively execute the chmod command on a folder in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template