Development

PHP stat() Function

In this tutorial, we will learn how we can use the stat() method in PHP to gather information about a specific file within the current filesystem.
Captain Salem 2 min read
PHP stat() Function

As developers and system administrators, we rely on information about our programs and operating systems to find and fix errors in various applications.

It is therefore to have a few troubleshooting skills under your belt to ensure that you can gather and determine the problems in a system without rely on external tools.

PHP stat() Function

As stated, this function allows us to gather information about a file. The function syntax is as shown below:

stat(string $filename): array|false

The function accepts one parameter:

  1. filename - this specifies the path to the target file.

The function will then return an array with various elements denoting the file information. The returned information is as demonstrated below:

Key Numeric Value Meaning
dev 0 Device number in which the file resides
ino 1 Inode number of the file
mode 2 File type and permissions
nlink 3 Number of hard links to the file
uid 4 User ID of the file owner
gid 5 Group ID of the file owner
rdev 6 Device type, if the file is a special file
size 7 Size of the file in bytes
atime 8 Time of last access
mtime 9 Time of last modification
ctime 10 Time of last status change
blksize 11 Block size used by the file system
blocks 12 Number of 512-byte blocks allocated to the file

Example Function Usage

The following code snippet shows the usage of the stat() function and the resulting output:

<?php
$file_path = '/home/d3b1an/.bashrc';

// Use the stat() function to get file information
$file_info = stat($file_path);

// Display the file size
echo "File size: " . $file_info['size'] . " bytes\n";

// Display the owner user ID and group ID
echo "Owner user ID: " . $file_info['uid'] . "\n";
echo "Owner group ID: " . $file_info['gid'] . "\n";

// Display the file permissions
echo "File permissions: " . decoct($file_info['mode'] & 0777) . "\n";

// Display the last access, modification, and status change times
echo "Last access time: " . date('Y-m-d H:i:s', $file_info['atime']) . "\n";
echo "Last modification time: " . date('Y-m-d H:i:s', $file_info['mtime']) . "\n";
echo "Last status change time: " . date('Y-m-d H:i:s', $file_info['ctime']) . "\n";
?>

The code above uses the stat() function to gather the file details about the .bashrc file.

We can run the code above:

$ php file_info.php

Resulting output:

File size: 3526 bytes
Owner user ID: 1000
Owner group ID: 1000
File permissions: 644
Last access time: 2023-04-23 23:56:28
Last modification time: 2023-04-23 23:56:24
Last status change time: 2023-04-23 23:56:24

Conclusion

In this tutorial, you discovered how you can monitor and gather information about a specific file within the filesystem using the stat() function in PHP.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.