
- PHP 教程
- PHP - 首頁
- PHP - 簡介
- PHP - 安裝
- PHP - 歷史
- PHP - 特性
- PHP - 語法
- PHP - Hello World
- PHP - 註釋
- PHP - 變數
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 變數
- PHP - 常量
- PHP - 魔術常量
- PHP - 資料型別
- PHP - 型別轉換
- PHP - 型別混淆
- PHP - 字串
- PHP - 布林值
- PHP - 整數
- PHP - 檔案與 I/O
- PHP - 數學函式
- PHP - Heredoc & Nowdoc
- PHP - 複合型別
- PHP - 檔案包含
- PHP - 日期與時間
- PHP - 標量型別宣告
- PHP - 返回型別宣告
- PHP 運算子
- PHP - 運算子
- PHP - 算術運算子
- PHP - 比較運算子
- PHP - 邏輯運算子
- PHP - 賦值運算子
- PHP - 字串運算子
- PHP - 陣列運算子
- PHP - 條件運算子
- PHP - 展開運算子
- PHP - 空值合併運算子
- PHP - 比較運算子(宇宙飛船運算子)
- PHP 控制語句
- PHP - 決策
- PHP - If…Else 語句
- PHP - Switch 語句
- PHP - 迴圈型別
- PHP - For 迴圈
- PHP - Foreach 迴圈
- PHP - While 迴圈
- PHP - Do…While 迴圈
- PHP - Break 語句
- PHP - Continue 語句
- PHP 函式
- PHP - 函式
- PHP - 函式引數
- PHP - 按值傳遞
- PHP - 按引用傳遞
- PHP - 預設引數
- PHP - 具名引數
- PHP - 可變引數
- PHP - 返回值
- PHP - 傳遞函式
- PHP - 遞迴函式
- PHP - 型別提示
- PHP - 變數作用域
- PHP - 嚴格型別
- PHP - 匿名函式
- PHP - 箭頭函式
- PHP - 可變函式
- PHP - 區域性變數
- PHP - 全域性變數
- PHP 超全域性變數
- PHP - 超全域性變數
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 檔案處理
- PHP - 檔案處理
- PHP - 開啟檔案
- PHP - 讀取檔案
- PHP - 寫入檔案
- PHP - 檔案是否存在
- PHP - 下載檔案
- PHP - 複製檔案
- PHP - 追加檔案
- PHP - 刪除檔案
- PHP - 處理 CSV 檔案
- PHP - 檔案許可權
- PHP - 建立目錄
- PHP - 列出檔案
- 面向物件的 PHP
- PHP - 面向物件程式設計
- PHP - 類和物件
- PHP - 建構函式和解構函式
- PHP - 訪問修飾符
- PHP - 繼承
- PHP - 類常量
- PHP - 抽象類
- PHP - 介面
- PHP - 特性
- PHP - 靜態方法
- PHP - 靜態屬性
- PHP - 名稱空間
- PHP - 物件迭代
- PHP - 封裝
- PHP - final 關鍵字
- PHP - 過載
- PHP - 克隆物件
- PHP - 匿名類
- PHP Web 開發
- PHP - Web 概念
- PHP - 表單處理
- PHP - 表單驗證
- PHP - 表單郵件/URL
- PHP - 完整表單
- PHP - 檔案包含
- PHP - GET & POST
- PHP - 檔案上傳
- PHP - Cookie
- PHP - Session
- PHP - Session 選項
- PHP - 傳送郵件
- PHP - 輸入過濾
- PHP - Post-Redirect-Get (PRG)
- PHP - 快閃記憶體訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 編碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - 除錯
- PHP - 針對 C 開發人員
- PHP - 針對 PERL 開發人員
- PHP - 框架
- PHP - Core PHP 與框架
- PHP - 設計模式
- PHP - 過濾器
- PHP - JSON
- PHP - 異常
- PHP - 特殊型別
- PHP - 雜湊
- PHP - 加密
- PHP - is_null() 函式
- PHP - 系統呼叫
- PHP - HTTP 認證
- PHP - 交換變數
- PHP - Closure::call()
- PHP - 過濾後的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 期望
- PHP - use 語句
- PHP - 整數除法
- PHP - 已棄用的特性
- PHP - 已移除的擴充套件和 SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 程序
- PHP - PDO 擴充套件
- PHP - 內建函式
- PHP 有用資源
- PHP - 速查表
- PHP - 問答
- PHP - 快速指南
- PHP - 線上編譯器
- PHP - 有用資源
- PHP - 討論
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 中的一個檔案系統函式,允許您更改檔案的組。它允許您透過分配合適的歸屬來管理檔案和目錄。