php chmod()函數與批量修改檔案目錄許可權

來源:互聯網
上載者:User
chmod() 函數改變檔案模式。chmod — Changes file mode 如果成功則返回 TRUE,否則返回 FALSE。

文法

chmod(file,mode)


參數 描述
file 必需。規定要檢查的檔案。
mode

可選。規定新的許可權。

mode 參數由 4 個數字組成:

  • 第一個數字永遠是 0

  • 第二個數字規定所有者的許可權

  • 第二個數字規定所有者所屬的使用者組的許可權

  • 第四個數字規定其他所有人的許可權

可能的值(如需設定多個許可權,請對下面的數字進行總計):

  • 1 - 執行許可權

  • 2 - 寫入權限

  • 4 - 讀許可權


代碼如下:

<?php chmod("/somedir/somefile", 755); // 十進位數,可能不對 chmod("/somedir/somefile", "u+rwx,go+rx"); // 字串,不對 chmod("/somedir/somefile", 0755); // 八位元,正確的 mode 值 ?>

改進遞迴檔案模式@ infosoft ....,這是一個小短,應處理的Linux檔案系統的所有檔案類型。這個可以批量變更檔或目錄的許可權

代碼如下:

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

如果你目錄太多的話可以用

代碼如下:

<?php $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST); foreach($iterator as $item) { chmod($item, $filemode); } ?>

這段代碼來修改目錄的許可權

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.