Thursday, July 11, 2024
HomeLanguagesPhpPHP | stat( ) function

PHP | stat( ) function

The stat() function in PHP is an inbuilt function which is used to return information of a file. The stat(0) function returns statistics of a file which is an array with the following elements :

  • [0] or [dev] – Device number
  • [1] or [ino] – Inode number
  • [2] or [mode] – Inode protection mode
  • [3] or [nlink] – Number of links
  • [4] or [uid] – User ID of owner
  • [5] or [gid] – Group ID of owner
  • [6] or [rdev] – Inode device type
  • [7] or [size] – Size in bytes
  • [8] or [atime] – Last access (as Unix timestamp)
  • [9] or [mtime] – Last modified (as Unix timestamp)
  • [10] or [ctime] – Last inode change (as Unix timestamp)
  • [11] or [blksize] – Blocksize of filesystem IO
  • [12] or [blocks] – Number of blocks allocated

The stat() function accepts the filename as a parameter and returns an array with the above-mentioned elements on success and False on failure.
If filename is a symbolic link then statistics are from the file itself, not the symlink.

Syntax:

stat(filename)

Parameters Used:
The stat() function in PHP accepts one parameter.

  1. filename : It specifies the filename of the file whose statistics you want to know.

Return Value:
It returns array with the above-mentioned elements on success and False on failure.

Errors And Exception

  1. The results of the stat() function differs from server to server.
  2. The result of the stat() function are cached and therefore the clearstatcache() function should be used to clear the cache.
  3. The stat() function generates an E_WARNING on failure.
  4. On Windows platforms the groupid of owner, userid of owner and inode number are always 0.
  5. For files which are larger than 2GB some filesystem functions may return unexpected results since PHP’s integer type is signed and many platforms use 32bit integers.

Examples:

Input : $test = stat('gfg.txt');
        echo 'Access time: ' .$test['atime'];
        echo '
Modification time: ' .$test['mtime']; echo '
Device number: ' .$test['dev']; Output :Access time: 1141666750 Modification time: 1135897503 Device number: 0 Input : $test = stat('gfg.txt'); echo 'Access time: ' .$test[8]; echo '
Modification time: ' .$test[9]; echo '
Device number: ' .$test[0]; Output : Access time: 1141666750 Modification time: 1135897503 Device number: 0 Input : $test = stat('gfg.txt'); $access_time = $stat['atime'] + 18000; if (touch($test, time(), $access_time)) { echo 'Access time changed to 5 hours in the past!'; } else { echo 'Access time could not be changed.'; } Output : Access time changed to 5 hours in the past!

Below programs illustrate the stat() function.

Suppose there is a file named “gfg.txt”

Program 1




<?php
$test = stat('gfg.txt');
//using stat() along with name index to display access time
echo 'Access time: ' .$test['atime'];
  
//using stat() along with name index  to display modification time
echo '<br />Modification time: ' .$test['mtime'];
  
//using stat() along with name index  to display device number
echo '<br />Device number: ' .$test['dev'];
?>


Output:

Access time: 1141666750
Modification time: 1135897503
Device number: 0

Program 2




<?php
$test = stat('gfg.txt');
  
//using stat() along with number index to display access time
echo 'Access time: ' .$test[8];
  
//using stat() along with number index to display modification time
echo '<br />Modification time: ' .$test[9];
  
//using stat() along with number index to display device number
echo '<br />Device number: ' .$test[0];
?>


Output:

Access time: 1141666750
Modification time: 1135897503
Device number: 0

Program 3




<?php
$test = stat('gfg.txt');
  
//changing access time to 5 hours in the past
$access_time = $stat['atime'] + 18000;
  
//using touch() function to change the access time
if (touch($test, time(), $access_time)) 
{
   echo 'Access time changed to 5 hours in the past!';
else 
{
   echo 'Access time could not be changed.';
}
  
?>


Output:

Access time changed to 5 hours in the past!

Reference:
http://php.net/manual/en/function.stat.php

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments