PHP 檔案系統 chgrp() 函式



PHP 檔案系統 chgrp() 函式可以用來更改檔案組。該函式嘗試將由 `filename` 指定的檔案的組更改為指定的 `group`。只有超級使用者可以隨意將組更改為任何組,而其他使用者只能將其更改為他們所屬的組。

檔案/目錄中的組含義:在基於 Unix 的作業系統中,檔案控制系統會規範誰有權訪問檔案/資料夾以及他們被允許對檔案/資料夾執行哪些操作。每個檔案/資料夾都有三種類型的使用者:所有者(擁有它的人)、組(共享相同許可權的人)和其他。

語法

以下是 PHP 檔案系統 chgrp() 函式的語法:

bool chgrp ( string $filename, string/int $group )

引數

以下是 chgrp() 函式的必需引數和可選引數:

序號 引數及說明
1

filename (必需)

它定義了您想要更改其使用者組的檔案。

2

group (必需)

它標識新的使用者組。它可以是組名或數字。

返回值

此函式在成功時返回 TRUE,失敗時返回 FALSE。

PHP 版本

chgrp() 函式最初作為 PHP 4 核心的一部分引入,並且與 PHP 5、PHP 7、PHP 8 相容良好。

示例

在下面的 PHP 程式碼中,使用 PHP 檔案系統 chgrp() 函式獲取檔案“sample.txt”的組 ID。之後,它列印更新的組 ID 和時間戳,並將檔案的組更改為“admin”。

<?php
   $filename = "/PhpProject/sample.txt";
   $format = "%s's Group ID @ %s: %d\n";
   printf($format, $filename, date('r'), filegroup($filename));
   chgrp($filename, "admin");
   clearstatcache();  // do not cache filegroup() results
   printf($format, $filename, date('r'), filegroup($filename));
?>

輸出

這將產生以下結果:

/PhpProject/sample.txt's Group ID @ Fri, 23 May 2024 02:42:21 +0200: 0
/PhpProject/sample.txt's Group ID @ Fri, 23 May 2024 02:42:21 +0200: 0

示例

下面的 PHP 程式碼在使用 chgrp() 函式將組設定為“admin”後,迭代陣列中的每個檔案,並列印每個檔案的更新的組 ID 和時間戳。

在這裡,我們還使用了 clearstatcache() 函式來清除檔案資訊的快取。

<?php
   // Specify the filenames here
   $files = ["/PhpProject/sample1.txt", "/PhpProject/sample2.txt"];

   // define format of the file
   $format = "File: %s, Group ID @ %s: %d\n";
   
   foreach ($files as $filename) {
       printf($format, $filename, date('r'), filegroup($filename));
       chgrp($filename, "admin");
       clearstatcache();
       printf($format, $filename, date('r'), filegroup($filename));
   }

?> 

輸出

這將生成以下結果:

Permissions for /PhpProject/sample.txt @ Tue, 23 May 2024 02:53:00 +0000: 100644
Permissions for /PhpProject/sample.txt @ Tue, 23 May 2024 02:53:01 +0000: 100644

示例

我們還可以使用 chgrp() 函式同時更改多個檔案的組所有權。為此,我們必須將兩個路徑定義為檔名陣列。

<?php
   // specify the file path here
   $files = ["/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt", "/Applications/XAMPP/xamppfiles/htdocs/mac/newfile.txt"];
   
   $format = "File: %s, Group ID @ %s: %d\n";
   
   foreach ($files as $filename) {
       printf($format, $filename, date('r'), filegroup($filename));
       echo "<br>";  // Print a new line for better readability
       chgrp($filename, "admin");
       clearstatcache();
       printf($format, $filename, date('r'), filegroup($filename));
       echo "<br>";  // Print a new line for better readability
   }
   
?> 

輸出

這將導致以下結果:

File: /Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt, Group ID @ Thu, 23 May 2024 14:44:34 +0200: 80
File: /Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt, Group ID @ Thu, 23 May 2024 14:44:34 +0200: 80
File: /Applications/XAMPP/xamppfiles/htdocs/mac/newfile.txt, Group ID @ Thu, 23 May 2024 14:44:34 +0200: 80
File: /Applications/XAMPP/xamppfiles/htdocs/mac/newfile.txt, Group ID @ Thu, 23 May 2024 14:44:34 +0200: 80

示例

現在,我們將根據給定的條件使用 chgrp() 函式更改組所有權。因此,程式碼只檢查和更正如果現有組所有權不是由“admin”擁有的檔案。

該程式碼還使用了 posix_getgrnam() 函式來檢索資訊,filename() 函式來獲取檔名,filegroup() 函式來獲取檔案的組。

<?php
   // specify the file path here
   $filename = "/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt";

   $groupToSet = "admin";
   $format = "File: %s, Group ID @ %s: %d\n";

   printf($format, $filename, date('r'), filegroup($filename));
   echo "<br>";

   if (filegroup($filename) != posix_getgrnam($groupToSet)['gid']) {
      chgrp($filename, $groupToSet);
      clearstatcache();
   }

   printf($format, $filename, date('r'), filegroup($filename));
?> 

輸出

這將產生以下結果:

File: /Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt, Group ID @ Thu, 23 May 2024 14:51:48 +0200: 80
File: /Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt, Group ID @ Thu, 23 May 2024 14:51:48 +0200: 80

注意

  • 您必須具有更改檔案組的必要許可權。
  • 在某些系統上,您可能需要成為超級使用者 (root) 才能更改檔案的組。

總結

chgrp() 是 PHP 中的一個檔案系統函式,允許您更改檔案的組。它允許您透過分配合適的歸屬來管理檔案和目錄。

php_function_reference.htm
廣告