What is the stat() function in PHP?

stat() is a built-in function in PHP.

The stat() function returns information (or statistics) regarding a specific file. We can extract a variety of file-specific information using the stat() function.

The stat() function

Syntax

The following is the prototype of the function:

stat(filename)

Parameters and return value

The stat() function takes the following parameter:

  • filename: A string representing the name of the file to be examined.

The function returns an array that contains different information regarding the file at each index. The elements within the array may be accessed via their indices or a string key.

The following table gives a breakdown of the arrays returned by the stat() function:

Index

Key

Value

0

dev

Device number

1

ino

Inode number

2

mode

Inode protection mode

3

nlink

Number of links

4

uid

Owner's User ID

5

gid

Owner's Group ID

6

rdev

Inode device type

7

size

Size of the file (in bytes)

8

atime

Last access

9

mtime

Last modified

10

ctime

Last inode change

11

blksize

Blocksize of filesystem

12

blocks

Number of blocks allocated

Code

The code below demonstrates how we can use the stat function.

main.php
Edpresso.txt
<?php
// Ivoking stat() --> array stored in $info
$info = stat('Edpresso.txt');
// Using key to get access time
echo "Access time: ", $info['atime'];
// Using index to get file size
echo "\nFile size: ", $info[7];
?>

Note: The last access, modified, and inode change values are returned as Unix timestamps. This is the number of seconds that have elapsed since 00:00:00 UTC on January 1st, 1970.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved