Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See the date last accessed a directory php

Tags:

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!

like image 892
Axll Avatar asked Dec 26 '22 05:12

Axll


2 Answers

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

fileatime('dir');
like image 171
rid Avatar answered Jan 07 '23 02:01

rid


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

like image 36
George Reith Avatar answered Jan 07 '23 02:01

George Reith