stat

(PHP 4, PHP 5, PHP 7, PHP 8)

stat給出文件的信息

說明

stat(string $filename): array

獲取由 filename 指定的文件的統(tǒng)計信息。如果 filename 是符號連接,則統(tǒng)計信息是關(guān)于被連接文件本身的,而不是符號連接。

lstat()stat() 相同,只除了它會返回符號連接的狀態(tài)。

參數(shù)

filename

文件的路徑。

返回值

stat()fstat() 返回格式
數(shù)字下標 關(guān)聯(lián)鍵名(自 PHP 4.0.6) 說明
0 dev device number - 設(shè)備名
1 ino inode number - inode 號碼
2 mode inode protection mode - inode 保護模式
3 nlink number of links - 被連接數(shù)目
4 uid userid of owner - 所有者的用戶 id
5 gid groupid of owner- 所有者的組 id
6 rdev device type, if inode device * - 設(shè)備類型,如果是 inode 設(shè)備的話
7 size size in bytes - 文件大小的字節(jié)數(shù)
8 atime time of last access (unix timestamp) - 上次訪問時間(Unix 時間戳)
9 mtime time of last modification (unix timestamp) - 上次修改時間(Unix 時間戳)
10 ctime time of last change (unix timestamp) - 上次改變時間(Unix 時間戳)
11 blksize blocksize of filesystem IO * - 文件系統(tǒng) IO 的塊大小
12 blocks number of blocks allocated - 所占據(jù)塊的數(shù)目
* Windows 下總是 0。

* - 僅在支持 st_blksize 類型的系統(tǒng)下有效。其它系統(tǒng)(如 Windows)返回 -1。

如果出錯,stat() 返回 false。

注意: 因為 PHP 的整數(shù)類型是有符號整型而且很多平臺使用 32 位整型,對 2GB 以上的文件,一些文件系統(tǒng)函數(shù)可能返回無法預期的結(jié)果。

錯誤/異常

錯誤時會產(chǎn)生 E_WARNING 級別的錯誤。

更新日志

版本 說明
4.0.6 返回一個數(shù)組包含有文件的統(tǒng)計信息,該數(shù)組具有以下列出的單元,數(shù)組下標從零開始。除了數(shù)字索引之外自還可以通過關(guān)聯(lián)索引來訪問。

范例

示例 #1 stat() 例子

<?php
/* Get file stat */
$stat stat('C:\php\php.exe');

/*
 * Print file access time, this is the same 
 * as calling fileatime()
 */
echo 'Access time: ' $stat['atime'];

/*
 * Print file modification time, this is the 
 * same as calling filemtime()
 */
echo 'Modification time: ' $stat['mtime'];

/* Print the device number */
echo 'Device number: ' $stat['dev'];
?>

示例 #2 Using stat() information together with touch()

<?php
/* Get file stat */
$stat stat('C:\php\php.exe');

/* Did we failed to get stat information? */
if (!$stat) {
    echo 
'stat() call failed...';
} else {
    
/*
     * We want the access time to be 1 week 
     * after the current access time.
     */
    
$atime $stat['atime'] + 604800;

    
/* Touch the file */
    
if (!touch('some_file.txt'time(), $atime)) {
        echo 
'Failed to touch file...';
    } else {
        echo 
'touch() returned success...';
    }
}
?>

注釋

注意:

注意:不同文件系統(tǒng)對時間的判斷方法可能是不相同的。

注意: 此函數(shù)的結(jié)果會被緩存。參見 clearstatcache() 以獲得更多細節(jié)。

小技巧

自 PHP 5.0.0 起, 此函數(shù)也用于某些 URL 包裝器。請參見 支持的協(xié)議和封裝協(xié)議以獲得支持 stat() 系列函數(shù)功能的包裝器列表。

參見

  • lstat() - 給出一個文件或符號連接的信息
  • fstat() - 通過已打開的文件指針取得文件信息
  • filemtime() - 取得文件修改時間
  • filegroup() - 取得文件的組