PHP Filesystem filemtime() Function



The PHP Filesystem filemtime() function is used to return the last time file content was modified. So basically it returns the last change time as Unix timestamp on success or false on failure.

This function can return time when the data blocks of a file written to, that is, the time when the content of the file has changed.

Syntax

Below is the syntax of the PHP Filesystem filemtime() function −

int filemtime ( string $filename )

Parameters

Followings is the parameter of this function −

Sr.No Parameter & Description
1

filename(Required)

The file that will be scanned.

Return Value

The function returns the last modification time of the file as a Unix timestamp on success and if the file is not present or there is an error, it will return FALSE.

PHP Version

The filemtime() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.

Example

In the below example code we will use the PHP Filesystem filemtime() function to get the last modification time and date of the file mentioned inside the function.

As this function retrieves the recent modification time as a Unix timestamp. So we will convert it into an easily readable date and time string.

<?php
   echo filemtime("/PhpProject/sample.txt"); 
   echo "\n";
   echo "Last modified: ".date("F d Y H:i:s.",filemtime("/PhpProject/sample.txt"));
?>

Output

This will lead to the following outcome −

1590392449
Last modified: May 25 2020 09:40:49.

Example

This example shows us how you can use filemtime() function to check the age of a file by comparing its modification time with the current time.

<?php
   // Get the modification time of the file (replace the file path with your file)
   $modTime = filemtime("/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt");

   // Get the current time
   $currentTime = time();

   // Get the age of the file in seconds
   $fileAge = $currentTime - $modTime;

   // change seconds to days
   $fileAgeInDays = floor($fileAge / (60 * 60 * 24));

   // echo the age of the file
   echo "File age: $fileAgeInDays days";
?> 

Output

This will create the below outcome −

File age: 3 days

Example

This example shows us how we can use filemtime() function to get the file version by appending the modification timestamp to the file name.

And with the help of the rename() function, the old file is renamed to the new filename along with the modification timestamp.

<?php
   // Define the file path 
   $filename = "/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt";

   //Get the modification time
   $modTime = filemtime($filename);

   // Append modification timestamp to filename
   $newFilename = basename($filename, ".txt") . "_v$modTime.txt";

   // Rename the file
   rename($filename, $newFilename);

   echo "File renamed to: $newFilename";
?> 

Output

This will generate the below result −

File renamed to: myfile_v1716886151.txt

Example

This example shows how to simply compare a file's modification time to make sure there have not been any changes. So we will use a loop to compare between the initial modification time and the current one.

If the modification time changes, the loop finishes and a message displaying "File has been modified!" will be printed.

<?php
   // Define the file path 
   $filename = "/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt";
   
   $initialModTime = filemtime($filename);
   
   while (true) {
      // Clear the file status cache
      clearstatcache(); 
      $currentModTime = filemtime($filename);
   
      if ($currentModTime != $initialModTime) {
            echo "File has been modified!";
            break;
      }
   
      // Wait for 5 seconds before checking again
      sleep(5); 
   }
?> 

Output

This will produce the following result −

File has been modified!

Note

It is frequently used in conditional expressions like if statements or ternary operators for error handling because it returns false on failure.

Summary

The filemtime() is a useful tool for PHP developers to work with file operations, whether they are using it for version check, basic checks, file age, or monitoring.

php_function_reference.htm
Advertisements