0

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)

Edmhs
  • 3,645
  • 27
  • 39
  • What you ask for depends heavily on file-system configurations. Which file-system are you using for the directories in question? – hakre Jun 18 '11 at 17:20
  • Possible duplicate of: [Rules for “Date Modified” of folders in Windows Explorer](http://stackoverflow.com/questions/1025187/rules-for-date-modified-of-folders-in-windows-explorer) – hakre Jun 18 '11 at 17:22
  • when u mean edit folder u mean delete files or change in files,right? – lovesh Jun 18 '11 at 17:26
  • When i add new file, delete file, add folders/delete them, date changes, but when i edit file in folder, for example edit some php file, taht file parent folder modifies dat time is not changed – Edmhs Jun 18 '11 at 17:34

2 Answers2

4

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.

lovesh
  • 5,235
  • 9
  • 62
  • 93
2

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__));
seriousdev
  • 7,519
  • 8
  • 45
  • 52