PHP stat() Function

What is PHP stat() Function?

If you want to know file specific information about a file like last access time, its size, last modification time etc. call PHP stat() function. Here, “stat” is short for “statistics”.

Syntax:

stat(filename)

Parameters:

The Function has 1 parameter which is required-

filename (Required): it is the path to the file.

Return Values:

The function returns-

  • File information as an array – on success and
  • False – if there happens any error.

In the array, the function returns the following information with the defined keys-

Numeric KeyAssociative keyDescription
0devDevice number on which the file is located.
1infoInode number (identification number) of the file
2modeInode protection mode (File type and permissions)
3nlinkNumber of links
4uiduserid of owner
5gidgroupid of owner
6rdevDevice type, if inode device
7sizeSize in bytes
8atimeTime of last access (Unix timestamp)
9mtimetime of last modification (Unix timestamp)
10ctimetime of last inode change (Unix timestamp)
11blksizeblocksize of filesystem IO
12blocksnumber of 512-byte blocks allocated

Examples:

Example 1:

<pre>
<?php
print_r(stat("file/demo.txt"));
?>
</pre>

Output:

Array

(

    [0] => 3568259165

    [1] => 53480245575052405

    [2] => 33206

    [3] => 1

    [4] => 0

    [5] => 0

    [6] => 0

    [7] => 425

    [8] => 1709522847

    [9] => 1708619782

    [10] => 1708614606

    [11] => -1

    [12] => -1

    [dev] => 3568259165

    [ino] => 53480245575052405

    [mode] => 33206

    [nlink] => 1

    [uid] => 0

    [gid] => 0

    [rdev] => 0

    [size] => 425

    [atime] => 1709522847

    [mtime] => 1708619782

    [ctime] => 1708614606

    [blksize] => -1

    [blocks] => -1
)

Explanation:

The stat() function returns the file specific detail information as elements of an array. It contains same information in both numeric key and string key.

Example 2:

<?php
$file = stat('file/demo.txt');
$access_timestamp = $file['atime'];
echo "The file was last accesssed on: " . date("n/j/Y g:i:s a", $access_timestamp);
echo "<br />";
$modify_timestamp = $file['mtime'];
echo "The file was last modified on: " . date("n/j/Y g:i:s a", $modify_timestamp);
?>

Output:

The file was last accessed on: 3/3/2024 10:27:27 pm
The file was last modified on: 2/22/2024 11:36:22 am

Explanation:

Line 3: The last access time of the file can be found by the associative key “atime”. We can also get the access time by the index key 8 like $file[8]. It returns the timestamp of the access time. So, we convert this timestamp by date() function to readable date format.

Line 6: The last modification time of the file can be found by the associative key “mtime”. We can also get the modification time by the index key 9 like $file[9]. It returns the timestamp of the modification time. So, we convert this timestamp by date() function to readable date format.

Notes on stat() Function:

  • On Windows, as of PHP 7.4.0, device number is the serial number of the volume that contains the file, which is a 64-bit unsigned integer, so may overflow. Previously, it was the numeric representation of the drive letter (e.g. 2 for C:) for stat(), and 0 for lstat().
  • On Windows, as of PHP 7.4.0, inode number is the identifier associated with the file, which is a 64-bit unsigned integer, so may overflow. Previously, it was always 0.
  • On Windows, the writable permission bit is set according to the read-only file attribute, and the same value is reported for all users, group and owner. The ACL is not taken into account, contrary to is_writable().
  • On Windows uid and gid will always be 0.
  • blksize and blocks are valid on systems supporting the st_blksize type – other systems (e.g. Windows) return -1.
  • The value of mode(associative array key) contains information read by several functions. When written in octal, starting from the right, the first three digits are returned by chmod() function. The next digit is ignored by PHP. The next two digits indicate the file type:
    Mode in octalMeaning
    0140000Socket
    0120000Link
    0100000Regular file
    0060000Block device
    0040000Directory
    0020000Character device
    0010000Fifo

    For example, a regular file could be 0100644. So, as the last three digits are 644, so, the permission for the owner, group, and others are 6 (Read+Write), 4 (Read), and 4 (Read) respectively. Also, sixth and fifth digits from the right side are 10, so its a regular file (see the above table).

Caution:

PHP cache the result of the stat() function. So if you call this function multiple times in a script PHP sends you the cached result. The problem is if existence of the directory, you won’t get the up-to-date result. To get the latest result, clear the cache with clearstatcache() function. Check the code below-

<?php
$path = "css/";
var_dump(stat($path));
// Some codes.
var_dump(stat($path));
// Some codes.
clearstatcache();
var_dump(stat($path));
// Some codes.
?>

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP stat() Function

Sometime, you need to know file specific information of a file. Then, use this stat() function which is a built-in PHP fuilesystem function.

Reference:

https://www.php.net/manual/en/function.stat.php