fileatime()
The fileatime() function is a built-in PHP function that returns the last access time of a file. This function returns a Unix timestamp representing the time
What is the fileatime() Function?
The fileatime() function is a built-in PHP function that returns the last access time of a file. This function returns an integer Unix timestamp representing the time the file was last accessed.
Here's the basic syntax of the fileatime() function:
The PHP syntax of fileatime()
fileatime(filename);Where filename is the name of the file to be checked.
Important note: On modern Linux systems, mount options like noatime or relatime often prevent the access time from updating on every read. If the returned time does not change as expected, check your filesystem configuration.
How to Use the fileatime() Function?
Using the fileatime() function is straightforward. Here are the steps to follow:
- Call the
fileatime()function, passing in the name of the file you want to check. - The function will return an integer Unix timestamp representing the time the file was last accessed, 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 fileatime() function:
<?php
$filename = 'myfile.txt';
$last_access_time = fileatime($filename);
if ($last_access_time !== false) {
$access_time_string = date('F d Y H:i:s.', $last_access_time);
echo "The file $filename was last accessed on $access_time_string";
} else {
echo "Could not access file information for $filename.";
}In this example, we check when the file myfile.txt was last accessed using the fileatime() function. We store the integer Unix timestamp in the $last_access_time variable. We then verify the result is not false to prevent warnings when passing it to date(). Finally, we format the timestamp and output a message indicating when the file was last accessed.
Conclusion
The fileatime() function is a useful tool in PHP for checking when a file was last accessed. By following the steps outlined in this guide, you can easily use the fileatime() function in your PHP projects to check when files were last accessed. We hope this guide has been helpful, and we wish you the best of luck in your PHP endeavors!
Practice
What does the fileatime() function in PHP do?