PHP chmod 函數與批量修改檔案目錄許可權_PHP教程

來源:互聯網
上載者:User
文法
chmod(file,mode)參數 描述
file 必需。規定要檢查的檔案。
mode 可選。規定新的許可權。
mode 參數由 4 個數字組成:
第一個數字永遠是 0
第二個數字規定所有者的許可權
第二個數字規定所有者所屬的使用者組的許可權
第四個數字規定其他所有人的許可權
可能的值(如需設定多個許可權,請對下面的數字進行總計):
1 - 執行許可權
2 - 寫入權限
4 - 讀許可權
來看個簡單的執行個體
複製代碼 代碼如下:
chmod("/somedir/somefile", 755); // 十進位數,可能不對
chmod("/somedir/somefile", "u+rwx,go+rx"); // 字串,不對
chmod("/somedir/somefile", 0755); // 八位元,正確的 mode 值
?>

改進遞迴檔案模式@ infosoft ....,這是一個小短,應處理的Linux檔案系統的所有檔案類型。這個可以批量變更檔或目錄的許可權
複製代碼 代碼如下:
function chmodr($path, $filemode) {
if (!is_dir($path))
return chmod($path, $filemode);
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
if($file != '.' && $file != '..') {
$fullpath = $path.'/'.$file;
if(is_link($fullpath))
return FALSE;
elseif(!is_dir($fullpath) && !chmod($fullpath, $filemode))
return FALSE;
elseif(!chmodr($fullpath, $filemode))
return FALSE;
}
}
closedir($dh);
if(chmod($path, $filemode))
return TRUE;
else
return FALSE;
}
?>

如果你目錄太多的話可以用
複製代碼 代碼如下:
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $item) {
chmod($item, $filemode);
}
?>

這段代碼來修改目錄的許可權
哈哈,我們不只是講chmod簡單文法,而且還做了複雜的關於chmod使用執行個體

說明
bool chmod ( string $filename , int $mode )
嘗試將 filename 所指定檔案的模式改成 mode 所給定的。

注意 mode 不會被自動當成八位元值,而且也不能用字串(例如 "g+w")。要確保正確操作,需要給 mode 前面加上 0:

mode 參數包含三個八位元按順序分別指定了所有者、所有者所在的組以及所有人的訪問限制。每一部分都可以通過加入所需的許可權來計算出所要的許可權。數字 1 表示使檔案可執行,數字 2 表示使檔案可寫,數字 4 表示使檔案可讀。加入這些數字來制定所需要的許可權。有關 UNIX 系統的檔案許可權可以閱讀手冊“man 1 chmod”和“man 2 chmod”。
複製代碼 代碼如下:

// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);

// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);

// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);

// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
?>

如果成功則返回 TRUE,失敗則返回 FALSE。

Note: 目前使用者指的是執行 PHP 的使用者。很可能和通常的 shell 或者 FTP 使用者不是同一個。在大多數系統下檔案模式只能被檔案所有者的使用者改變。


Note: 本函數不能作用於遠程檔案,被檢查的檔案必須通過伺服器的檔案系統訪問。

Note: 當安全模式開啟的時候,PHP 會檢查所操作的檔案是否和正在執行的指令碼具有相同的 UID (所有者)。要注意的是,不能修改 SUID,SGID 和 sticky bits。

http://www.bkjia.com/PHPjc/321788.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321788.htmlTechArticle文法 chmod(file,mode)參數 描述 file 必需。規定要檢查的檔案。 mode 可選。規定新的許可權。 mode 參數由 4 個數字組成: 第一個數字永遠是 0 第二...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.