Backend Development 5 min read

How to Use PHP fileatime() to Get a File's Last Access Time

This article explains the PHP fileatime() function, its syntax, parameters, return value, and provides practical examples for retrieving a file's last access timestamp, formatting it, and using it for file management tasks such as cleanup based on inactivity.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP fileatime() to Get a File's Last Access Time

In PHP, the fileatime() function retrieves the last access time of a file, which is the moment the file was last read using operations like readfile() or fread() . By calling fileatime() , you can obtain an accurate UNIX timestamp representing this access time.

The basic syntax of fileatime() is:

int fileatime ( string $filename )

Parameter Description:

$filename (required): The path to the file whose last access time you want to retrieve.

Return Value:

The function returns a UNIX timestamp (in seconds) indicating the file's last access time.

The following example demonstrates how to use fileatime() :

$file = 'example.txt';

// Get the file's last access time
$lastAccessTime = fileatime($file);

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

// Output the result
echo 'File last accessed at: ' . $lastAccessTime;

This script sets a file path, obtains its last access timestamp, converts it to a human‑readable format with date() , and prints the result.

Because fileatime() returns a UNIX timestamp, you often need to use date() or similar functions to present the time in a conventional format.

Beyond simply retrieving the timestamp, fileatime() can be used for file management decisions, such as determining whether a file has not been accessed for a long period and performing cleanup. The example below shows how to delete a file if it hasn't been accessed for more than 30 days:

$file = 'example.txt';

// Get the file's last access time
$lastAccessTime = fileatime($file);

// Check if the file has been idle for over 30 days
if (time() - $lastAccessTime > 30 * 24 * 60 * 60) {
    // Perform cleanup, e.g., delete the file
    unlink($file);
    echo 'File has been deleted';
} else {
    echo 'File was accessed recently';
}

This code compares the current time with the file's last access time; if the difference exceeds 30 days, the file is removed, otherwise a message indicating recent access is shown.

Summary

Using fileatime() , you can easily obtain a file's last access time and, together with functions like date() , convert it for display or use it in logic for file management and cleanup tasks; the function is a valuable tool for backend file handling.

BackendPHPfile managementphp-functionsfile access timefileatime
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.