PHP Filesystem filemtime() Function (2024)

PHP Filesystem filemtime() Function (1)

  • PHP Tutorial
  • PHP - Home
  • PHP - Introduction
  • PHP - Installation
  • PHP - History
  • PHP - Features
  • PHP - Syntax
  • PHP - Hello World
  • PHP - Comments
  • PHP - Variables
  • PHP - Echo/Print
  • PHP - var_dump
  • PHP - $ and $$ Variables
  • PHP - Constants
  • PHP - Magic Constants
  • PHP - Data Types
  • PHP - Type Casting
  • PHP - Type Juggling
  • PHP - Strings
  • PHP - Boolean
  • PHP - Integers
  • PHP - Maths Functions
  • PHP - Compound Types
  • PHP - File Include
  • PHP - Scalar Type Declarations
  • PHP - Return Type Declarations
  • PHP Operators
  • PHP - Operators
  • PHP - Arithmatic Operators
  • PHP - Comparison Operators
  • PHP - Logical Operators
  • PHP - Assignment Operators
  • PHP - String Operators
  • PHP - Array Operators
  • PHP - Conditional Operators
  • PHP - Spread Operator
  • PHP - Null Coalescing Operator
  • PHP - Spaceship Operator
  • PHP Control Statements
  • PHP - Decision Making
  • PHP - If…Else Statement
  • PHP - Switch Statement
  • PHP - Loop Types
  • PHP - For Loop
  • PHP - Foreach Loop
  • PHP - While Loop
  • PHP - Do…While Loop
  • PHP - Break Statement
  • PHP - Continue Statement
  • PHP Arrays
  • PHP - Arrays
  • PHP - Indexed Array
  • PHP - Associative Array
  • PHP - Multidimensional Array
  • PHP - Array Functions
  • PHP - Constant Arrays
  • PHP Functions
  • PHP - Functions
  • PHP - Function Parameters
  • PHP - Call by value
  • PHP - Call by Reference
  • PHP - Default Arguments
  • PHP - Named Arguments
  • PHP - Variable Arguments
  • PHP - Returning Values
  • PHP - Passing Functions
  • PHP - Recursive Functions
  • PHP - Type Hints
  • PHP - Variable Scope
  • PHP - Strict Typing
  • PHP - Anonymous Functions
  • PHP - Arrow Functions
  • PHP - Variable Functions
  • PHP - Local Variables
  • PHP - Global Variables
  • PHP Superglobals
  • PHP - Superglobals
  • PHP - $GLOBALS
  • PHP - $_SERVER
  • PHP - $_REQUEST
  • PHP - $_POST
  • PHP - $_GET
  • PHP - $_FILES
  • PHP - $_ENV
  • PHP - $_COOKIE
  • PHP - $_SESSION
  • PHP File Handling
  • PHP - File Handling
  • PHP - Open File
  • PHP - Read File
  • PHP - Write File
  • PHP - File Existence
  • PHP - Download File
  • PHP - Copy File
  • PHP - Append File
  • PHP - Delete File
  • PHP - Handle CSV File
  • PHP - File Permissions
  • PHP - Create Directory
  • PHP - Listing Files
  • Object Oriented PHP
  • PHP - Object Oriented Programming
  • PHP - Classes and Objects
  • PHP - Constructor and Destructor
  • PHP - Access Modifiers
  • PHP - Inheritance
  • PHP - Class Constants
  • PHP - Abstract Classes
  • PHP - Interfaces
  • PHP - Traits
  • PHP - Static Methods
  • PHP - Static Properties
  • PHP - Namespaces
  • PHP - Object Iteration
  • PHP - Encapsulation
  • PHP - Final Keyword
  • PHP - Overloading
  • PHP - Cloning Objects
  • PHP - Anonymous Classes
  • PHP Web Development
  • PHP - Web Concepts
  • PHP - Form Handling
  • PHP - Form Validation
  • PHP - Form Email/URL
  • PHP - Complete Form
  • PHP - File Inclusion
  • PHP - File Uploading
  • PHP - Cookies
  • PHP - Sessions
  • PHP - Session Options
  • PHP - Sending Emails
  • PHP - Sanitize Input
  • PHP - Post-Redirect-Get (PRG)
  • PHP - Flash Messages
  • PHP AJAX
  • PHP - AJAX Introduction
  • PHP - AJAX Search
  • PHP - AJAX XML Parser
  • PHP - AJAX Auto Complete Search
  • PHP - AJAX RSS Feed Example
  • PHP XML
  • PHP - XML Introduction
  • PHP - Simple XML Parser
  • PHP - SAX Parser Example
  • PHP - DOM Parser Example
  • PHP Login Example
  • PHP - Login Example
  • PHP - Facebook Login
  • PHP - Paypal Integration
  • PHP - MySQL Login
  • PHP Advanced
  • PHP - MySQL
  • PHP.INI File Configuration
  • PHP - Array Destructuring
  • PHP - Coding Standard
  • PHP - Regular Expression
  • PHP - Error Handling
  • PHP - Try…Catch
  • PHP - Bugs Debugging
  • PHP - For C Developers
  • PHP - For PERL Developers
  • PHP - Frameworks
  • PHP - Core PHP vs Frame Works
  • PHP - Design Patterns
  • PHP - Filters
  • PHP - JSON
  • PHP - Exceptions
  • PHP - Special Types
  • PHP - Hashing
  • PHP - Encryption
  • PHP - is_null() Function
  • PHP - System Calls
  • PHP - HTTP Authentication
  • PHP - Swapping Variables
  • PHP - Closure::call()
  • PHP - Filtered unserialize()
  • PHP - IntlChar
  • PHP - CSPRNG
  • PHP - Expectations
  • PHP - Use Statement
  • PHP - Integer Division
  • PHP - Deprecated Features
  • PHP - PEAR
  • PHP - CSRF
  • PHP - FastCGI Process
  • PHP - PDO Extension
  • PHP - Built-In Functions
  • PHP Useful Resources
  • PHP - Quick Guide
  • PHP - Useful Resources
  • PHP - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

Previous

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.NoParameter & 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 −

1590392449Last 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

Print Page

Previous Next

Advertisem*nts

';adpushup.triggerAd(ad_id); });

PHP Filesystem filemtime() Function (2024)

References

Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6014

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.