当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP对文件夹递归执行chmod命令的方法

PHP对文件夹递归执行chmod命令的方法

2018年06月16日  | 移动技术网IT编程  | 我要评论

本文实例讲述了php对文件夹递归执行chmod命令的方法。分享给大家供大家参考。具体分析如下:

这里对文件夹和文件递归执行chmod命令来改变执行权限

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

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网