PHP | stat() Function

PHP stat() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP stat() Function.

The built-in PHP stat() function returns the information about a file.

Syntax

Following below is the syntax to use this function -

array stat ( string $filename )


This function gathers the statistics of the file named by the filename. If the filename is a symbolic link, statistics are from the file itself, not the symlink. The PHP lstat() function is identical to PHP stat() function, except that it could be based on symlink status.

Return Value

This function returns the statistics of a file as an array on success, or false on failure.

Example1

Try out the below example -

<?php
   $stat = stat("/PhpProject/sample.txt");  // Get file stat 

   echo "Acces time: " .$stat["atime"];    // Print file access time, this is the same as calling fileatime()  
   echo "\nModification time: " .$stat["mtime"];  //Print file modification time, this is the same as calling filemtime()
   echo "\nDevice number: " .$stat["dev"];  //  Print the device number
?>

Output

When the above code is executed, it will produce the following result -

Acces time: 1590217956
Modification time: 1591617832
Device number: 1245376677

Example2

Try out the below example -

<?php
   $stat = stat("/PhpProject/sample.txt");
   
   if(!$stat) {
      echo "stat() call failed...";
   } else {
      $atime = $stat["atime"] + 604800;

   if(!touch("/PhpProject1/sampl2.txt", time(), $atime)) {
      echo "failed to touch file...";
   } else {
      echo "touch() returned success...";
   }
?>

Output

When the above code is executed, it will produce the following result -

touch() returned success...


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP symlink() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.