2012-02-13 200 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__));