PHP stat() Function
The PHP stat() function returns information (statistics) about a file specified by filename. If filename is a symbolic link, information about the file itself is returned, not the symlink.
The results from this function will differ from server to server. The array may contain the number index, the name index, or both.
lstat() function is identical to this function except it would instead be based off the symlinks status.
Syntax
stat(filename)
Parameters
filename |
Required. Specify the file path. |
Return Value
In case of error, returns false, otherwise returns an array with the following elements:
Numeric | Associative | Description |
---|---|---|
0 | dev | device number |
1 | ino | inode number |
2 | mode | inode protection mode |
3 | nlink | number of links |
4 | uid | userid of owner |
5 | gid | groupid of owner |
6 | rdev | device type, if inode device |
7 | size | size in bytes |
8 | atime | time of last access (Unix timestamp) |
9 | mtime | time of last modification (Unix timestamp) |
10 | ctime | time of last inode change (Unix timestamp) |
11 | blksize | blocksize of filesystem IO |
12 | blocks | number of 512-byte blocks allocated |
The value of mode contains information read by several functions. When written in octal, starting from the right, the first three digits are returned by chmod(). The next digit is ignored by PHP. The next two digits indicate the file type. For example a regular file could be 0100644 and a directory could be 0040755.
mode file types
mode in octal | Description |
---|---|
0140000 | socket |
0120000 | link |
0100000 | regular file |
0060000 | block device |
0040000 | directory |
0020000 | character device |
0010000 | fifo |
Exceptions
Upon failure, an E_WARNING is emitted.
Example: stat() example
Lets assume that we have a file called test.txt. The example below describes how to use the stat() function to get the statistics about this file.
<?php $file = "test.txt"; //using the stat() function to //get statistics about the file $info = stat($file); //displaying the statistics print_r($info); ?>
The output of the above code will be:
Array ( [0] => 2049 [1] => 523021 [2] => 33188 [3] => 1 [4] => 33 [5] => 33 [6] => 0 [7] => 48 [8] => 1633620019 [9] => 1633620019 [10] => 1633620019 [11] => 4096 [12] => 8 [dev] => 2049 [ino] => 523021 [mode] => 33188 [nlink] => 1 [uid] => 33 [gid] => 33 [rdev] => 0 [size] => 48 [atime] => 1633620019 [mtime] => 1633620019 [ctime] => 1633620019 [blksize] => 4096 [blocks] => 8 )
Example: using stat() information together with touch() function
Consider one more example, where this function is used with touch() function.
<?php $file = "test.txt"; //using the stat() function to //get statistics about the file $info = stat($file); //changing the access time to 5 hours in the past $atime = $info['atime'] + 18000; //using touch() function to change the access time if (touch($file, time(), $atime)) { echo 'Access time changed to 5 hours in the past!'; } else { echo 'Access time could not be changed.'; } ?>
The output of the above code will be:
Access time changed to 5 hours in the past!
❮ PHP Filesystem Reference