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); } ?>
這段代碼來修改目錄的許可權