Axll
Axll

Reputation: 55

See the date last accessed a directory php

I was wondering if it where possible to see, with PHP, when the last time a folder was accessed. I was thinking about using 'touch()' in php but that's more for a file, isn't it?

Thanks in advance!

Upvotes: 4

Views: 485

Answers (4)

George Reith
George Reith

Reputation: 13476

As far as I know this information is only stored about files (According to others this is wrong and it is for directories - see Dinesh's answer). However you can iterate over each file in a directory and discover the most recently accessed file in the directory (Not exactly what you want but possibly as close as you will get). Using the DirectoryIterator:

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
$accessed = 0;
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        if ($fileinfo->getATime() > $accessed) {
            $accessed = $fileinfo->getAtime();
        }
    }
}
print($accessed);
?>

http://php.net/manual/en/directoryiterator.getatime.php

Upvotes: 4

rid
rid

Reputation: 63442

You can use fileatime(), which works for both files and directories:

fileatime('dir');

Upvotes: 4

Mihai Vilcu
Mihai Vilcu

Reputation: 1967

I would open the folder and loop through the files (you can use glob() for that) and grab the latest edited time with filemtime().

http://php.net/manual/en/function.filemtime.php

http://www.php.net/manual/en/function.glob.php

i can post a small example if you still need help

Upvotes: 0

Dinesh
Dinesh

Reputation: 3105

you can use stat function

$stat = stat('path to directory');
echo 'Accesstime: ' . $stat['atime']; // will show access unix time stamp.

Upvotes: 2

Related Questions