2012-02-13 194 views
12

我想递归设置文件夹和文件权限。文件夹应该得到750和文件644.我发现this并做了一些适应。这个会工作吗?递归地设置所有文件和文件夹的权限

<?php 

function chmod_r($Path) { 
    $dp = opendir($Path); 
    while($File = readdir($dp)) { 
     if($File != "." AND $File != "..") { 
     if(is_dir($File)){ 
      chmod($File, 0750); 
     }else{ 
      chmod($Path."/".$File, 0644); 
      if(is_dir($Path."/".$File)) { 
       chmod_r($Path."/".$File); 
      } 
     } 
     } 
    } 
    closedir($dp); 
} 

?> 

回答

24

为什么不使用查找工具呢?

exec ("find /path/to/folder -type d -exec chmod 0750 {} +"); 
exec ("find /path/to/folder -type f -exec chmod 0644 {} +"); 
+2

这对部分托管服务提供商不适用于PHP。真的需要使用PHP API来做到这一点(请参阅下面的答案)。 – 2014-02-16 11:19:56

+1

当您通过FTP意外设置对某些目录的权限过低(例如644)时,这非常有用 - 这是修复它的方法。 – 2014-08-18 22:15:29

+0

你能解释一下这个解决方案吗?我不想在没有信息的情况下更改文件权限 – 2015-05-26 09:55:55

2

我认为你的文件夹不会递归,我解决了这个问题。

function chmod_r($Path) { 
    $dp = opendir($Path); 
    while($File = readdir($dp)) { 
     if($File != "." AND $File != "..") { 
     if(is_dir($File)){ 
      chmod($File, 0750); 
      chmod_r($Path."/".$File); 
     }else{ 
      chmod($Path."/".$File, 0644); 
     } 
     } 
    } 
    closedir($dp); 
} 
+0

我认为这并不是真的递归递归。 is_dir()需要在绝对路径上完成,即is_dir($ path。“/".$file)。 – 2014-02-16 11:19:10

+0

@JiriKopsa你错了,PHP手册明确说**如果filename是一个相对文件名,它将被检查相对于当前工作目录。**。 * Priste mrkni手册;)*(http://php.net/manual/en/function.is-dir.php),并且不要忘记他以递归方式调用chmod_r,所以路径在“潜水”中被扩展... – 2014-08-18 21:48:26

13

这是测试和工程就像一个魅力:

<? 

    header('Content-Type: text/plain'); 

    /** 
    * Changes permissions on files and directories within $dir and dives recursively 
    * into found subdirectories. 
    */ 
    function chmod_r($dir, $dirPermissions, $filePermissions) { 
     $dp = opendir($dir); 
     while($file = readdir($dp)) { 
     if (($file == ".") || ($file == "..")) 
      continue; 

     $fullPath = $dir."/".$file; 

     if(is_dir($fullPath)) { 
      echo('DIR:' . $fullPath . "\n"); 
      chmod($fullPath, $dirPermissions); 
      chmod_r($fullPath, $dirPermissions, $filePermissions); 
     } else { 
      echo('FILE:' . $fullPath . "\n"); 
      chmod($fullPath, $filePermissions); 
     } 

     } 
    closedir($dp); 
    } 

    chmod_r(dirname(__FILE__), 0755, 0755); 
?> 
+0

非常适合我,谢谢! – 2014-12-02 22:51:58

+0

作品,档案应该是0644.库尔德:) – 2015-05-26 09:53:55

+0

完美答案!!! – SagarPPanchal 2017-05-18 18:37:28

16

我的解决方案将递归改变所有文件和文件夹,以0777我用DirecotryIterator,它的执行opendir和while循环更清洁的替代。

function chmod_r($path) { 
    $dir = new DirectoryIterator($path); 
    foreach ($dir as $item) { 
     chmod($item->getPathname(), 0777); 
     if ($item->isDir() && !$item->isDot()) { 
      chmod_r($item->getPathname()); 
     } 
    } 
} 
+3

Omg,我的代码只是一个示例。您可以将权限更改为任何您想要的。 – zener 2015-06-26 11:27:32

+3

有点像教'exec'和使用'exex('rm/* -rf');'作为一个例子,大声笑 – 2015-07-19 08:30:06

3

这里改进版本的递归chmod跳过具有相同权限的文件。

<? 

header('Content-Type: text/plain'); 

/** 
* Changes permissions on files and directories within $dir and dives recursively 
* into found subdirectories. 
*/ 
function chmod_r($dir) 
{ 
    $dp = opendir($dir); 
    while($file = readdir($dp)) 
    { 
     if (($file == ".") || ($file == "..")) continue; 

     $path = $dir . "/" . $file; 
     $is_dir = is_dir($path); 

     set_perms($path, $is_dir); 
     if($is_dir) chmod_r($path); 
    } 
    closedir($dp); 
} 

function set_perms($file, $is_dir) 
{ 
    $perm = substr(sprintf("%o", fileperms($file)), -4); 
    $dirPermissions = "0750"; 
    $filePermissions = "0644"; 

    if($is_dir && $perm != $dirPermissions) 
    { 
     echo("Dir: " . $file . "\n"); 
     chmod($file, octdec($dirPermissions)); 
    } 
    else if(!$is_dir && $perm != $filePermissions) 
    { 
     echo("File: " . $file . "\n"); 
     chmod($file, octdec($filePermissions)); 
    } 

    flush(); 
} 

chmod_r(dirname(__FILE__));