Edmhs
Edmhs

Reputation: 3731

Php - folder last modification datetime -

Im writing script that creates list of all files and folders, and when next time it runs, i wanted to check if folder is modified since last time, and if not, exclude folder from reading this time,

I use filemtime(), and stat() function,

It works, when i add new files in subfolders date is changing, but if i edit file in folder, folder last modified date id not changing, using these functions.

Is there a way how to check if files in folder have been edited, without going through all files one by one??

this script will run very often, so i dont want so it goes through all files and folders every time,(in some folders there is a lot of files)

Upvotes: 0

Views: 2038

Answers (2)

lovesh
lovesh

Reputation: 5401

filemtime() returns the time when the data blocks of a file were being written to, that is, the time when the content of the file was changed. now folder is also a special file which contains information about the files contained in the folder and what i understand is when u use filemtime() on a folder it shows when that when the folder(file)'s byte-size was changed. so when u add a file to the folder,an entry about the file is made in the folder(special file) and its byte-size changes but when u edit an already existing file the folder's(special file) byte-size doesnt change so filemtime() shows no change.

Upvotes: 4

seriousdev
seriousdev

Reputation: 7656

This could work:

function foldermtime($dir) {
    $foldermtime = 0;

    $flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_FILEINFO;
    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, $flags));

    while ($it->valid()) {
        if (($filemtime = $it->current()->getMTime()) > $foldermtime) {
            $foldermtime = $filemtime;
        }
        $it->next();
    }

    return $foldermtime ?: false;
}

var_dump(foldermtime(__DIR__));

Upvotes: 2

Related Questions