Logging all changes in a directory

Discussion in 'PHP' started by wvccboy, Aug 24, 2007.

  1. #1
    Hey

    I need a PHP script or function that can display all changes made to any files within the directory.
    For example if I edit index.php, this function will show me "$variable edited" when called up.

    Any good ideas on this?

    Thanks
     
    wvccboy, Aug 24, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    To achieve this, you would either have to edit the files via a script that logs every change made, or you need a database with all file information and a script that goes through the database each time it's called and compares the file values with the values in the table.

    Unless you have a good reason to do this, I think it's a silly thing. Do you have several people working on your files and you want to know if something has been edited?

    Maybe something like this is enough, which would show all files in a directory that have been edited within the past X days.

    
    <?php
    
    $path = '.';
    $days = 3;
    $now = time();
    
    foreach (glob("{$path}/*.*") AS $file)
    {	
    	if (filemtime($file) > ($now - 3600 * 24 * $days))
    	{
    		echo "{$file} has been edited within the last {$days} days.<br />\n";
    	}
    }
    
    ?>
    
    PHP:
     
    nico_swd, Aug 25, 2007 IP
    wvccboy and ErectADirectory like this.
  3. wvccboy

    wvccboy Notable Member

    Messages:
    2,632
    Likes Received:
    81
    Best Answers:
    1
    Trophy Points:
    250
    #3
    Thanks, tried that but it tells me glob() is undefined... Yes I have many people working on this website, which is why I just need to show what has been edited in the past..

    I'm not too experienced with the glob() function; how can I fix that?

    Thanks!
     
    wvccboy, Aug 25, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You must be using an old version of PHP (tell your host to upgrade)

    Alternatively you can use the good old opendir() version though:
    
    <?php
    
    $path = './path/to/files/';
    $days = 3;
    
    // Don't edit below
    
    $dir = @opendir($path) OR die("Cannot open directory: {$path}");
    $now = time();
    
    while (($file = readdir($dir)) !== false)
    {   
    	$file = "{$path}{$file}";
    	$lastedit = filemtime($file);
    
        if (is_file($file) AND $lastedit > ($now - 3600 * 24 * $days))
        {
            printf("<a href=\"%s\">%s</a> has been edited within the last %d days (%s)<br />\n", $file, $file, $days, date('dS M @ H:i:s', $lastedit));
    	}
    }
    
    closedir($dir);
    
    ?>
    
    PHP:
     
    nico_swd, Aug 25, 2007 IP
  5. wvccboy

    wvccboy Notable Member

    Messages:
    2,632
    Likes Received:
    81
    Best Answers:
    1
    Trophy Points:
    250
    #5
    Works perfectly! Yes I have php 4.3.2 which is why it had the error then.

    Thanks!
     
    wvccboy, Aug 25, 2007 IP