English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP stat() Function Usage and Example

PHP Filesystem Reference Manual

The stat() function can return information about the file.

Syntax

array stat ( string $filename )

This function can collect statistical information about the file named filename. If the filename is a symbolic link, the statistical information comes from the file itself, not the symbolic link. The lstat() function is the same as the stat() function, but it can be based on the symbolic link status.

Example1

<?php
   $stat = stat("/PhpProject/sample.txt");  //Get file status
   echo "Access time: " . $stat["atime"];    //Print file access time, which is the same as calling fileatime()
   echo "\nModification time: " . $stat["mtime"];  //Print file modification time, which is the same as calling filemtime()
   echo "\nDevice number: " . $stat["dev"];  // Print device number
?>

Output Result

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

Example2

<?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 Result

touch() returned success...

PHP Filesystem Reference Manual