当前位置:首页 > PHP教程 > PHP高级教程

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

本文实例讲述了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程序设计有所帮助。


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!