filectime()
The filectime() function is a built-in PHP function that returns the last change time of a file. This function returns a Unix timestamp representing the time
What is the filectime() Function?
The filectime() function is a built-in PHP function that returns the inode change time of a file — a Unix timestamp (the number of seconds since January 1, 1970) representing the moment the file's metadata was last changed. "Inode" is the data structure a Unix-like filesystem uses to store information about a file (its permissions, owner, size, and the location of its data blocks) separately from the file's actual contents.
A common point of confusion: filectime() is not the file's creation time. The "c" stands for change, not create. The inode change time updates whenever the file's metadata changes — for example when you run chmod, chown, rename the file, or even when you edit its contents (which also bumps the metadata). PHP has no portable function for a true creation time.
Use the right function for what you actually need:
filectime()— when the inode (metadata) last changed (permissions, ownership, link count, rename).filemtime()— when the file's contents were last modified.fileatime()— when the file was last accessed (read).
Here's the basic syntax of the filectime() function:
The PHP syntax of filectime()
filectime(string $filename): int|falseWhere $filename is the path of the file to be checked. The function returns the inode change time as a Unix timestamp, or false (with an E_WARNING) if the file does not exist or cannot be accessed. Because false and a timestamp can both look "falsy" in loose comparisons (a timestamp is never 0 in practice, but defensive code matters), always check the result with the strict !== false operator.
How to Use the filectime() Function?
Using the filectime() function is straightforward. Here are the steps to follow:
- Call the
filectime()function, passing in the name of the file you want to check. - The function will return a Unix timestamp representing the inode change time, or
falseon failure. - You can format the Unix timestamp using the
date()function to display the time in a more human-readable format.
Here's an example code snippet that demonstrates how to use the filectime() function:
How to Use the filectime() Function?
<?php
$filename = 'myfile.txt';
$last_change_time = filectime($filename);
if ($last_change_time !== false) {
$change_time_string = date('F d Y H:i:s', $last_change_time);
echo "The file $filename had its inode changed on $change_time_string";
} else {
echo "Could not retrieve inode change time for $filename.";
}Note: The filename parameter accepts both relative and absolute paths. If you use a relative path, it is resolved relative to the current working directory.
In this example, we check the inode change time of myfile.txt using the filectime() function. We store the returned Unix timestamp in the $last_change_time variable. The code first verifies the function did not return false, then formats the timestamp using date(). Note that date() relies on the server's default timezone; use date_default_timezone_set() if you need specific timezone output. Finally, we output a message indicating when the file's metadata was last changed.
A Self-Contained, Runnable Example
The snippet above depends on a file existing on disk. The example below creates a temporary file, changes its permissions to force an inode change, then reads the timestamp back — so you can run it as-is and see real output:
<?php
// Create a temporary file
$path = tempnam(sys_get_temp_dir(), 'demo');
file_put_contents($path, 'hello');
// Changing permissions updates the inode change time (ctime)
chmod($path, 0644);
$ctime = filectime($path);
echo "ctime: " . date('Y-m-d H:i:s', $ctime) . PHP_EOL;
echo "Is it a Unix timestamp (integer)? " . (is_int($ctime) ? 'yes' : 'no') . PHP_EOL;
unlink($path); // clean upThis prints the formatted change time followed by Is it a Unix timestamp (integer)? yes, confirming that filectime() returns an integer timestamp.
Watch Out For Stat Caching
PHP caches the result of filesystem-stat functions like filectime(), filemtime(), and fileperms() for the duration of a single request to improve performance. If you change a file and immediately call filectime() on it again in the same script, you may get the stale, cached value. Call clearstatcache() first to get a fresh reading:
<?php
$path = tempnam(sys_get_temp_dir(), 'demo');
$first = filectime($path);
sleep(1);
chmod($path, 0600); // changes the inode
clearstatcache(); // discard the cached stat result
$second = filectime($path);
echo ($second >= $first) ? "ctime updated\n" : "still cached\n";
unlink($path);This prints ctime updated. Remove the clearstatcache() line and PHP may report the old value instead.
Conclusion
The filectime() function is a useful tool in PHP for checking when a file's inode (metadata) was last changed — permissions, ownership, or a rename. Remember that despite the "c", it is not a creation-time function: use filemtime() when you need the last content-modification time, fileatime() for the last access time, and call clearstatcache() when you need a guaranteed-fresh reading within the same request. Pair it with file_exists() to avoid warnings on missing files.