phpfilelastaccesstime

How do I find the last access time of a file in php


I need to know when the last time a file (main.html) was accessed using PHP. I want the user be able to see when the last time a different user accessed the page. How can I do this using PHP?


Solution

  • The function fileatime is used to get the last access time of the given file.

    $filename = 'somefile.txt';
        if (file_exists($filename)) {
            echo "$filename was last accessed: " . date("F d Y H:i:s.", fileatime($filename));
        }
    
    // outputs e.g.  somefile.txt was last accessed: December 29 2002 22:16:23.