...

View Full Version : Detecting directory changes



bauhsoj
03-02-2007, 04:12 PM
I have a script that gets a large list of files from various directories. It would be great if I could determine if any files in that directory had been changed (or added) just by reading a directory attribute so that I don't have to rescan the directory every time.

Does anyone know if that is possible?

mlseim
03-02-2007, 05:49 PM
This is all I can think of (see below) ...

It sort of scans the directory anyhow, but maybe it's more efficient
than what you're doing. It looks for the total byte size. You'll have to
compare it with a previously saved size to know if it's changed or not.

Once you determine it has changed, you need to re-save the new size so
you can compare it again later on.

If you have your own server, there are some other things you could do.

Like this:
$dir = "/some/dir/";
$res = `df -c $dir`;
print $res;

or this ...

$dir = "/path/to/dir";
$size_in_bytes = disk_total_space($dir);
$size_in_k = $size_in_bytes / 1024;

Again, those are only if you have your own server.

Here's the script I found:


<?php
$path = './mydir/';
echo dirsize($path);

function dirsize($dir) {
$dirs = array($dir);
while(!empty($dirs)) { // Loop through all directories
$path = array_shift($dirs);

foreach(glob($path.'/*') AS $next) {
if(is_dir($next)) {
$dirs[] = $next; // Add another directory to the loop
} else {
$size += filesize ($next); // Add filesize to count
}
}
}
return $size; // Return the total filesize of the directory
}
?>

aedrin
03-02-2007, 05:52 PM
Size is not a good indicator of whether anything changed. It's not that uncommon for a file to change, and the size to remain the same.

The only way I see you can do this is by either having the program/script that generates those files to add some sort of mark, or by scanning each file for their last modified date.

bauhsoj
03-02-2007, 06:53 PM
I am trying to avoid scanning the directories period. There are about 100K of files in each directory and that puts some overhead on a script that I would like to get rid of if nothing in the directory has changed. I would actually be satisfied with just knowing if the file count has changed so that I know whether to scan that particular directory again. This would be simple if I could just check the directory's date modified against a stored last date modified.

Inigoesdr
03-02-2007, 07:29 PM
I am trying to avoid scanning the directories period. There are about 100K of files in each directory and that puts some overhead on a script that I would like to get rid of if nothing in the directory has changed. I would actually be satisfied with just knowing if the file count has changed so that I know whether to scan that particular directory again. This would be simple if I could just check the directory's date modified against a stored last date modified.

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

mlseim
03-02-2007, 07:32 PM
bauhsoj,

Is this within your own server or a webhost account?

I think knowing that would really changes things here.

bauhsoj
03-02-2007, 07:33 PM
http://www.php.net/manual/en/function.filemtime.php

So this works with directories? :)

Inigoesdr
03-02-2007, 07:38 PM
Yep. Look in the comments for a few examples, or google the function. =]

aedrin
03-02-2007, 09:04 PM
Learn something new every day. I remember the directory not reflecting the most recent change.

Just remember to follow the comments on that page. Files modified in subdirectories do not update the modified time of the parent directory.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum