W3docs

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 the fileatime() Function Does

The fileatime() function is a built-in PHP function that returns the last access time of a file — the moment its contents were last read. The value comes back as an integer Unix timestamp (the number of seconds since January 1, 1970, UTC), which you typically format with date() before showing it to a user.

This page covers the syntax, the return value, a runnable example, how access time differs from the related modification and change times, and the caching gotcha that trips up most people.

Syntax

fileatime(string $filename): int|false
  • $filename — the path to the file you want to inspect. It can be relative (resolved against the script's working directory) or absolute.
  • Return value — an integer Unix timestamp on success, or false on failure (for example, the file does not exist or you lack permission to stat it). Because false is a valid PHP value, always compare with !== false rather than a loose truthiness check.

Heads-up about Linux access times. On modern Linux systems, mount options such as noatime or relatime deliberately stop the access time from updating on every read, to save disk writes. If fileatime() does not change after you read a file, that is the filesystem configuration at work — not a bug in your code.

How to Use fileatime()

  1. Call fileatime(), passing the path of the file you want to check.
  2. Check the result against false before using it, so a missing file does not produce a warning when you pass it to date().
  3. Format the timestamp with date() to turn it into a human-readable string.
<?php

// Create a small file so the example is self-contained and runnable.
$filename = 'example.txt';
file_put_contents($filename, "Hello, fileatime!\n");

// Read it back to register an access.
$contents = file_get_contents($filename);

$lastAccess = fileatime($filename);

if ($lastAccess !== false) {
    $when = date('Y-m-d H:i:s', $lastAccess);
    echo "The file {$filename} was last accessed on {$when}.\n";
} else {
    echo "Could not read the access time for {$filename}.\n";
}

unlink($filename); // tidy up

This script creates example.txt, reads it, then asks fileatime() when it was last accessed. The !== false guard means a missing or unreadable file is handled gracefully instead of emitting a warning. The output looks like:

The file example.txt was last accessed on 2026-06-21 10:42:07.

fileatime() vs. filemtime() vs. filectime()

PHP exposes three closely related timestamps. Picking the right one matters:

FunctionReturnsUpdated when…
fileatime()Last access timeThe file's contents are read
filemtime()Last modification timeThe file's contents are written/changed
filectime()Last inode change (status change) timeThe contents or metadata (permissions, owner, rename) change

If you want "when did someone last open this file," use fileatime(). If you want "when did the data last change" (the common case for cache invalidation), use filemtime().

The Caching Gotcha

PHP caches the result of file-status functions like fileatime(), filemtime(), and stat() for the duration of a request. If you read a file again and re-check its access time within the same script run, you may get a stale value. Clear the cache with clearstatcache() before re-reading:

<?php

$filename = 'example.txt';
file_put_contents($filename, "data\n");

$first = fileatime($filename);

clearstatcache();                       // force a fresh stat() next time
$contents = file_get_contents($filename);
clearstatcache();
$second = fileatime($filename);

echo ($second >= $first)
    ? "Access time is fresh.\n"
    : "Access time looks stale.\n";

unlink($filename);

When Would I Use This?

  • Auditing which files have been read recently (log rotation, "least recently used" cleanup).
  • Detecting unused uploads or temp files you can safely delete.
  • Diagnosing whether a process is actually touching a file you expect it to.

For modification-based logic (rebuilding a cache when source data changes), reach for filemtime() instead. To check that a file exists before calling any of these, pair them with file_exists(). For the full set of metadata in one call, see stat().

Summary

fileatime() returns the Unix timestamp of a file's last access, or false on failure. Always guard the result with !== false, format it with date(), and remember two pitfalls: Linux mounts may suppress access-time updates, and PHP caches stat results within a request unless you call clearstatcache().

Practice

Practice
What does the fileatime() function in PHP do?
What does the fileatime() function in PHP do?
Was this page helpful?