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

怪我咯
怪我咯Original
2017-07-05 10:26:121509browse

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

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!

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