Home >Backend Development >PHP Tutorial >PHP recursively executes the chmod command on a folder

PHP recursively executes the chmod command on a folder

WBOY
WBOYOriginal
2016-07-25 08:45:05929browse
  1. function recursiveChmod($path, $filePerm=0644, $dirPerm=0755)
  2. {
  3. // Check if the path exists
  4. if(!file_exists($path))
  5. {
  6. return(FALSE);
  7. }
  8. // See whether this is a file
  9. if(is_file($path))
  10. {
  11. // Chmod the file with our given filepermissions
  12. chmod($path, $filePerm);
  13. // If this is a directory...
  14. } elseif(is_dir($path)) {
  15. // Then get an array of the contents
  16. $foldersAndFiles = scandir($path);
  17. // Remove "." and ".." from the list
  18. $entries = array_slice($foldersAndFiles, 2);
  19. // Parse every result...
  20. foreach($entries as $entry)
  21. {
  22. // And call this function again recursively, with the same permissions
  23. recursiveChmod($path."/".$entry, $filePerm, $dirPerm);
  24. }
  25. // When we are done with the contents of the directory, we chmod the directory itself
  26. chmod($path, $dirPerm);
  27. }
  28. // Everything seemed to work out well, return TRUE
  29. return(TRUE);
  30. }
  31. ?>
复制代码

PHP, chmod


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