定義和用法
PHP chmod() 函式改變檔案模式。
如果成功則返回 TRUE,否則返回 FALSE。
語法
可能的值(如需設定多個許可權,請對下面的數字進行總計):
參數 | 描述 |
file | 必需。規定要檢查的檔案。 |
mode | 可選。規定新的許可權。 mode 參數由 4 個數字組成: |
第一個數字永遠是 0 |
第二個數字規定所有者的許可權 |
第三個數字規定所有者所屬的用戶組的許可權 |
第四個數字規定其他所有人的許可權 |
1 - 執行許可權 |
2 - 寫許可權 |
4 - 讀許可權 |
例子
<?php // 所有者可讀寫,其他人沒有任何許可權 chmod("test.txt",0600); // 所有者可讀寫,其他人可讀 chmod("test.txt",0644); // 所有者有所有許可權,其他所有人可讀和執行 chmod("test.txt",0755); // 所有者有所有許可權,所有者所在的組可讀 chmod("test.txt",0740); ?> |
實例
批量更改檔案或目錄的許可權
<?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 Filesystem 函式