Current Location: Home> Latest Articles> PHP fileatime() Function Explained: How to Retrieve the Last Access Time of a File

PHP fileatime() Function Explained: How to Retrieve the Last Access Time of a File

gitbox 2025-06-13

PHP fileatime() Function Explained: How to Retrieve the Last Access Time of a File

In PHP, the fileatime() function is used to retrieve the last access time of a file. This time refers to when the file was last read or accessed, i.e., when operations such as readfile() or fread() were last performed. By using the fileatime() function, we can accurately retrieve the last access time of a file and take actions accordingly.

Basic Syntax of the fileatime() Function

int fileatime(string $filename)

Parameter Explanation:

  • filename: Required. Specifies the file path to get the last access time of.

Return Value:
The fileatime() function returns a UNIX timestamp representing the last access time of the file (in seconds).

Example Usage of the fileatime() Function

Next, let's demonstrate how to use the fileatime() function with the following example:

$file = 'example.txt'; // File path
// Get the last access time of the file
$lastAccessTime = fileatime($file);
// Convert the last access time to a human-readable date-time string
$lastAccessTime = date('Y-m-d H:i:s', $lastAccessTime);
// Output the last access time
echo 'The last access time of the file is: ' . $lastAccessTime;

In the example above, we first specify the file path 'example.txt' and use the fileatime() function to get its last access time. Then, we use the date() function to convert the last access time into a more readable date-time string and finally use echo to output the formatted string.

Note that since the fileatime() function returns a UNIX timestamp, we need to use PHP's date() function or other related functions to convert it to a commonly used date-time format.

Using fileatime() for File Management

In addition to retrieving the last access time of a file, we can use the fileatime() function to implement other features. For example, we can perform certain checks based on the last access time of a file, such as determining if the file has not been accessed for a long time, which can help with file cleanup or management. Below is an example:

$file = 'example.txt'; // File path
// Get the last access time of the file
$lastAccessTime = fileatime($file);
// Check if the file has not been accessed for over 30 days
if (time() - $lastAccessTime > 30 * 24 * 60 * 60) {
    // Perform some cleanup, such as deleting the file
    unlink($file);
    echo 'File has been deleted';
} else {
    echo 'File has been accessed recently';
}

In the example above, we first retrieve the file's last access time and then calculate the difference between the current time and the last access time. If the difference exceeds 30 days, we perform a cleanup operation like deleting the file. Otherwise, we display a message indicating the file has been accessed recently.

Summary

With the fileatime() function, we can easily retrieve the last access time of a file and perform further actions or checks as needed. Whether for retrieving file access times or managing files, the fileatime() function is a very useful tool.

We hope this article helps readers better understand and use the fileatime() function.