导航首页 » 技术教程 » PHP:stat()的用法_Filesystem函数
PHP:stat()的用法_Filesystem函数 113 2023-12-15   

stat

(PHP 4, PHP 5)

stat — 给出文件的信息

说明

array stat ( string $filename )

获取由 filename 指定的文件的统计信息。如果 filename 是符号连接,则统计信息是关于被连接文件本身的,而不是符号连接。

lstat() 和 stat() 相同,只除了它会返回符号连接的状态。

参数

filename

文件的路径。

返回值

stat() 和 fstat() 返回格式 数字下标 关联键名(自 PHP 4.0.6) 说明 0 dev device number - 设备名 1 ino inode number - inode 号码 2 mode inode protection mode - inode 保护模式 3 nlink number of links - 被连接数目 4 uid userid of owner - 所有者的用户 id 5 gid groupid of owner- 所有者的组 id 6 rdev device type, if inode device * - 设备类型,如果是 inode 设备的话 7 size size in bytes - 文件大小的字节数 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 * - 文件系统 IO 的块大小 12 blocks number of blocks allocated - 所占据块的数目 * Windows 下总是 0。

* - 仅在支持 st_blksize 类型的系统下有效。其它系统(如 Windows)返回 -1。

如果出错,stat() 返回 FALSE

Note: 因为 PHP 的整数类型是有符号整型而且很多平台使用 32 位整型,对 2GB 以上的文件,一些文件系统函数可能返回无法预期的结果 。

错误/异常

错误时会产生 E_WARNING 级别的错误。

更新日志

版本 说明 4.0.6 返回一个数组包含有文件的统计信息,该数组具有以下列出的单元,数组下标从零开始。除了数字索引之外自还可以通过关联索引来访问。

范例

Example #1 stat() 例子

<?php
/* Get file stat */
$stat = stat('C:phpphp.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'];
?>

Example #2 Using stat() information together with touch()

<?php
/* Get file stat */
$stat = stat('C:phpphp.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...';
    }
}
?>

注释

Note:

注意:不同文件系统对时间的判断方法可能是不相同的。

Note: 此函数的结果会被缓存。参见 clearstatcache() 以获得更多细节。

Tip

自 PHP 5.0.0 起, 此函数也用于某些 URL 包装器。请参见 支持的协议和封装协议以获得支持 stat() 系列函数功能的包装器列表。

参见

lstat() - 给出一个文件或符号连接的信息 fstat() - 通过已打开的文件指针取得文件信息 filemtime() - 取得文件修改时间 filegroup() - 取得文件的组


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103