當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP chmod()用法及代碼示例

chmod() 函數更改文件模式。它在成功時返回 TRUE,在失敗時返回 FALSE。

用法

chmod($file_path, file_mode)

參數

  • file_path −設置要檢查的文件或目錄的路徑。必需的。

  • file_mode −用值設置模式。 file_mode 參數說明如下

文件模式參數

使用以下四個值設置文件模式。

  • zero
  • 所有者的許可
  • 所有者用戶組的權限
  • 休息的權限

以下是設置多個權限的值。您需要添加以下數字 -

  • 1 = 執行權限
  • 2 = 寫權限
  • 4 = 讀取權限

返回

file_exists() 方法返回。

  • 是的,關於成功
  • 錯誤,失敗時

示例

以下是更改文件 “one.txt” 模式的示例。這為所有者設置了讀寫權限,其他人沒有。

<?php
   // Setting mode for file
   // Read and write permission for owner, nothing for everybody else
   chmod("one.txt",0600);
?>

讓我們看另一個更改 “two.txt” 文件模式的示例。這為所有者設置了讀寫權限,為其他人設置了讀取權限。

<?php
   // Setting mode for file
   // Read and write permission for owner, read for everybody else
   chmod("two.txt",0644);
?>

讓我們看另一個更改 “three.txt” 文件模式的示例。這為其他所有人設置了所有者、讀取和執行的所有權限。

<?php
   // Setting mode for file
   // All the permissions for owner, read and execute for everybody else
   chmod("three.txt",0755);
?>

讓我們看另一個更改 “four.txt” 文件模式的示例。這將設置所有者的所有權限,讀取所有者的組。

<?php
   // Setting mode for file
   // All the permissions for owner, read for owner's group
   chmod("four.txt",0740);
?>

讓我們看另一個更改 “five.txt” 文件模式的示例。這為所有者的組設置了所有者、讀取和執行的所有權限。

<?php
   // Setting mode for file
   // All the permissions for owner, read and execute for owner's group
   chmod("five.txt",0740);
?>

相關用法


注:本文由純淨天空篩選整理自Karthikeya Boyini大神的英文原創作品 chmod() function in PHP。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。