filectime()
What is the filectime() Function?
The filectime() function is a built-in PHP function that returns the inode change time of a file. This function returns a Unix timestamp representing the time the file's metadata (such as permissions or ownership) was last changed. Note that filectime() does not track content modifications; for the last content modification time, use filemtime() instead.
Here's the basic syntax of the filectime() function:
The PHP syntax of filectime()
filectime(filename);Where filename is the name of the file to be checked.
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.
Conclusion
The filectime() function is a useful tool in PHP for checking when a file's inode or metadata was last changed. By following the steps outlined in this guide, you can easily use filectime() in your PHP projects. Remember to use filemtime() if you specifically need the last content modification time.
Practice
What does the filectime() function in PHP do?