Determine When a File Was Last Updated With PHP
PHP can determine when a file was last updated. It's done with the filemtime()
function.
After describing the function and some things that can be done with it, I'll provide a short PHP script to delete all files in a directory that were last updated more than a specific number of days ago. (Code similar to that script is used at willmaster.com to delete old log and backup files.)
The filemtime()
function requires the exact location of the file being tested. Example:
filemtime($_SERVER['DOCUMENT_ROOT'].'/subdirectory/file.csv')
Alternatively, to specify the file location of the current PHP script or web page, when that location might change, the current file location can be specified this way (variable $_SERVER["PHP_SELF"]
contains the current location of the PHP script or web page):
filemtime($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF'])
The filemtime()
function returns the Unix timestamp of the very second the file was last updated. The timestamp is the number of seconds that have elapsed since January 1, 1970.
PHP code following the filemtime()
call can then use that timestamp for whatever the software is designed to do with it. In this article, I'll publish the timestamp for this page and also, further below, show how it can be used to determine which files are older than a certain number of days.
Immediately below the published timestamp, you'll see the exact PHP code this web page uses to publish it.
Here is the timestamp. It represents the second the file of this web page was last updated.
1734451293
Here is the code (filemtime()
function colored blue):
<?php echo( filemtime($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']) ) ?>
If the file was last updated before September 9, 2001, the timestamp will have 9 digits. Otherwise, 10 digits.
The timestamp number is tough for humans to decipher. So you probably won't publish the timestamp on your web pages, unless it's an instructional page about this very subject.
But a human-readable date and time can be composed from the timestamp, formatted for easy reading with the PHP date()
function.
Here is a date and time composed from the timestamp with the date()
function. (As before, the PHP code following the date is exactly as used on this web page.)
2024-12-17 10:01:33
Here is the code (date format colored red and the filemtime()
function colored blue):
<?php echo( date('Y-m-d H:i:s',filemtime($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF'])) ) ?>
That's still rather cryptic. So let's try this.
Tuesday, December 17, 2024 at 10:01:33 AM
Here is the code (as before, the date format colored red and the filemtime()
function colored blue):
<?php echo( date('l, F j, Y \a\t g:i:s A',filemtime($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF'])) ) ?>
Virtually any visible format you would want can be accommodated.
The PHP date() function page contains a list of available date()
formatting codes. The willmaster.com date and time formatting tool can be used for more intuitive formatting.
Using filemtime()
I make a lot of log files, pretty much of every activity, in case something unusual is noticed and I need to find out what happened and how. And, of course, being the backup advocate that I am, there are lots of backup files.
As you can imagine, files accumulate.
When directories end up with thousands of files, you know it's time to purge the old ones.
It's possible to manually delete old log and backup files every week or so. But if it's automated, you don't have to remember to do it manually. Purging scripts can be launched with a cron schedule if you don't have another way to do it automatically.
The following few lines of code delete all files that are older than a specified number of days. The deletion happens in the same directory where the script is installed.
<?php
$NumberOfDaysToKeepFiles = 30;
$cutoff = time() - ($NumberOfDaysToKeepFiles * 24 * 60 * 60);
foreach( glob('*') as $file )
{
// If it is a file, not a directory or alias [is_file()]
// and if the file date is less than the cutoff time [filemtime()]
// and if the file isn't this script [strpos()],
// then delete the file [unlink()].
if( is_file($file) and
filemtime($file) < $cutoff and
strpos($_SERVER['PHP_SELF'],$file) === false )
{
unlink($file);
}
}
?>
The $NumberOfDaysToKeepFiles = 30;
line is where you specify how many days you want to keep the files. Everything older than the number of days specified here is deleted when the script is run.
To specify the number of days, change 30
to the number of days appropriate for your needs.
The script can be named whatever you please, so long as it ends with the .php
file name extension.
Running the script will permanently delete files. There is no "trash" where deleted files can be restored from. So make any backups you need before you run this script.
After installation, type the script's URL into your browser to run it manually. That's to verify it works as it should.
The script can then be scheduled to run at certain times with cron or some other method used to launch it automatically. (Mine are launched when I first load my personal portal page every day.)
Those are two uses for the code that lets you know when a file was last updated, visual printing of the date and time, and deleting files older than a certain number of days.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager