📜  PHP | stat()函数

📅  最后修改于: 2022-05-13 01:56:28.960000             🧑  作者: Mango

PHP | stat()函数

PHP中的 stat()函数是一个内置函数,用于返回文件的信息。 stat(0)函数返回文件的统计信息,该文件是一个包含以下元素的数组:

  • [0] 或 [dev] – 设备编号
  • [1] 或 [ino] – Inode 编号
  • [2] 或 [mode] – inode 保护模式
  • [3] 或 [nlink] – 链接数
  • [4] 或 [uid] – 所有者的用户 ID
  • [5] 或 [gid] – 所有者的组 ID
  • [6] 或 [rdev] – Inode 设备类型
  • [7] 或 [size] – 大小(以字节为单位)
  • [8] 或 [atime] – 上次访问(作为 Unix 时间戳)
  • [9] 或 [mtime] – 最后修改时间(作为 Unix 时间戳)
  • [10] 或 [ctime] – 最后一次 inode 更改(作为 Unix 时间戳)
  • [11] 或 [blksize] – 文件系统 IO 的块大小
  • [12] 或 [blocks] – 分配的块数

stat()函数接受文件名作为参数,并在成功时返回一个包含上述元素的数组,在失败时返回 False。
如果文件名是符号链接,则统计信息来自文件本身,而不是符号链接。

句法:

stat(filename)

使用的参数:
PHP中的 stat()函数接受一个参数。

  1. filename :它指定您想知道其统计信息的文件的文件名。

返回值:
它在成功时返回带有上述元素的数组,在失败时返回 False。

错误和异常

  1. stat()函数的结果因服务器而异。
  2. stat()函数的结果被缓存,因此应使用 clearstatcache()函数清除缓存。
  3. stat()函数在失败时生成 E_WARNING。
  4. 在 Windows 平台上,所有者的 groupid、所有者的 userid 和 inode 编号始终为 0。
  5. 对于大于 2GB 的文件,一些文件系统函数可能会返回意外结果,因为 PHP 的整数类型是有符号的,并且许多平台使用 32 位整数。

例子:

Input : $test = stat('gfg.txt');
        echo 'Access time: ' .$test['atime'];
        echo 'Modification time: ' .$test['mtime'];
        echo 'Device number: ' .$test['dev'];

Output :Access time: 1141666750
        Modification time: 1135897503
        Device number: 0

Input : $test = stat('gfg.txt');
        echo 'Access time: ' .$test[8];
        echo 'Modification time: ' .$test[9];
        echo 'Device number: ' .$test[0];

Output : Access time: 1141666750
         Modification time: 1135897503
         Device number: 0

Input : $test = stat('gfg.txt');
        $access_time = $stat['atime'] + 18000;
        if (touch($test, time(), $access_time)) 
        {
          echo 'Access time changed to 5 hours in the past!';
        } 
        else 
        {
          echo 'Access time could not be changed.';
        }

Output : Access time changed to 5 hours in the past!

下面的程序说明了 stat()函数。

假设有一个名为“gfg.txt”的文件

程序 1

Modification time: ' .$test['mtime'];
  
//using stat() along with name index  to display device number
echo '
Device number: ' .$test['dev']; ?>

输出:

Access time: 1141666750
Modification time: 1135897503
Device number: 0

节目二

Modification time: ' .$test[9];
  
//using stat() along with number index to display device number
echo '
Device number: ' .$test[0]; ?>

输出:

Access time: 1141666750
Modification time: 1135897503
Device number: 0

方案 3


输出:

Access time changed to 5 hours in the past!

参考:
http://php.net/manual/en/函数PHP 。 PHP