How to Use PHP’s stat() Function to Retrieve Detailed File Information

Learn how PHP’s stat() function returns a comprehensive associative array of file attributes—such as size, device ID, inode, permissions, timestamps, and block information—by explaining its syntax, parameters, returned keys, and providing practical code examples for extracting and displaying these details.

php Courses
php Courses
php Courses
How to Use PHP’s stat() Function to Retrieve Detailed File Information

Stat() Function Overview

The stat() function in PHP obtains detailed information about a file, returning an associative array that includes size, timestamps, permissions, and other filesystem attributes.

Stat() Function Syntax

array stat(string $filename)

The single parameter $filename is the path to the file whose information you want to retrieve.

Returned Value

The function returns an associative array where each key corresponds to a specific file attribute.

Array Keys and Their Meanings

dev : Device ID where the file resides.

ino : Inode number of the file.

mode : File type and permission bits.

nlink : Number of hard links to the file.

uid : User ID of the file owner.

gid : Group ID of the file owner.

rdev : Device ID (if the file is a special file).

size : Size of the file in bytes.

atime : Time of last access.

mtime : Time of last modification.

ctime : Time of last status change.

blksize : Preferred block size for filesystem I/O.

blocks : Number of 512‑byte blocks allocated to the file.

Example Usage

<?php
$filename = 'test.txt';
$file_info = stat($filename);

echo 'File size: ' . $file_info['size'] . ' bytes' . PHP_EOL;

echo 'Last access time: ' . date('Y-m-d H:i:s', $file_info['atime']) . PHP_EOL;

echo 'Last modification time: ' . date('Y-m-d H:i:s', $file_info['mtime']) . PHP_EOL;

echo 'Last change time: ' . date('Y-m-d H:i:s', $file_info['ctime']) . PHP_EOL;
?>

This script prints the file size and the formatted timestamps for the last access, modification, and status change of test.txt.

Conclusion

The stat() function provides a convenient way to access a wide range of file metadata in PHP. By examining the returned associative array, developers can retrieve specific attributes such as size, permissions, and timestamps for use in file‑handling logic.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPFilesystemstatfile metadata
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.