Current Location: Home> Latest Articles> PHP function fileatime(): How to Retrieve File's Last Access Time and Process It

PHP function fileatime(): How to Retrieve File's Last Access Time and Process It

M66 2025-06-13

PHP function fileatime(): How to Retrieve File's Last Access Time and Process It

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, such as when functions like readfile() or fread() were called. By using the fileatime() function, you can easily retrieve the last access time of a file and perform the required operations.

Basic Syntax of fileatime() Function

The basic syntax of fileatime() is as follows:

int fileatime ( string $filename )

Parameter Explanation:

  • $filename: Required. Specifies the file path for which the last access time is to be retrieved.

Return Value:

fileatime() returns a UNIX timestamp representing the file's last access time (in seconds).

Example: Retrieve File's Last Access Time

Here is an example that shows how to use the fileatime() function to retrieve a file's last access time:

$file = 'example.txt'; // File path
// Get the file's last access time
$lastAccessTime = fileatime($file);

// Format the last access time into a human-readable date-time string
$lastAccessTime = date('Y-m-d H:i:s', $lastAccessTime);

// Output the last access time
echo 'The file\'s last access time is: ' . $lastAccessTime;

In this example, we first specify the file path 'example.txt'. Then, we use the fileatime() function to retrieve the file's last access time. Next, we use the date() function to convert the timestamp into a more readable date-time format, and finally, we use echo to output this formatted string.

Note that since fileatime() returns a UNIX timestamp, we usually need to use PHP's date() function or other date-related functions to convert it into a more common date format.

Other Applications: File Management Based on Access Time

fileatime() can also be used for file management based on the access time. For example, we can determine if a file hasn't been accessed for a long time and take appropriate action, such as deleting the file.

$file = 'example.txt'; // File path
// Get the file's last access time
$lastAccessTime = fileatime($file);

// Check if the file hasn't been accessed for more than 30 days
if (time() - $lastAccessTime > 30 * 24 * 60 * 60) {
    // Perform cleanup operation, such as deleting the file
    unlink($file);
    echo 'File has been deleted';
} else {
    echo 'File has been accessed recently';
}

In this example, we first get the file's last access time and calculate the difference between the current time and the last access time. If the file hasn't been accessed for more than 30 days, we delete it; otherwise, we output a message saying the file has been accessed recently.

Conclusion

With the fileatime() function, PHP developers can easily retrieve a file's last access time and perform further processing based on it. Whether it's simply retrieving the time or using it for file management or cleanup tasks, the fileatime() function is a valuable tool.

We hope this article helps you better understand and utilize the fileatime() function in PHP.