stat 给出文件的信息

发表日期:2021-07-01 08:55:45 | 来源: | | 浏览(790) 分类:文件系统函数

stat

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

stat给出文件的信息

说明

stat(string $filename): array

获取由 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

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

错误/异常

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

更新日志

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

范例

示例 #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...';
    }
}
?>

注释

注意:

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

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

小技巧

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

参见

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

集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。