Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Detecting directory changes

    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?
    Last edited by bauhsoj; 03-02-2007 at 04:01 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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 Code:
    <?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
    }
    ?>

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by bauhsoj View Post
    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

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    bauhsoj,

    Is this within your own server or a webhost account?

    I think knowing that would really changes things here.

  • #7
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So this works with directories?

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Yep. Look in the comments for a few examples, or google the function. =]

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •