Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


php help, compare two files on a set time interval and save one of the files if they are not equal.
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

php help, compare two files on a set time interval and save one of the files if they are not equal.

RaymiiRaymii Member
edited September 2012 in Help

For my monitoring software. I want to compare the status.json files every five minutes. If there is a difference I want to save the (older) file in the history folder. This took me 2 minutes in bash, but I've been struggling with it the whole day in php.

Function 1, gets called when a status.json file is parsed.

function savefile($bestand,$naam){

    $curdir=getcwd();
    if(!is_dir("{$curdir}/history")){
        mkdir("${curdir}/history") or die("Cannot create history folder. Create it manually and make sure the webserver can write to it.");
    } else {
        $DATETIME=date('U');
        $DATEHOUR=date('H');
        $DATEINT=date('i');

        if (filemtime("${curdir}/${naam}") < (time() - 300)) {
            $local_file=file_get_contents($bestand);
            $saved_local_file=file_put_contents("${curdir}/$naam", $local_file);
        } else {
            $local_file=file_get_contents($naam);
        }

        if ($DATEINT==00 || $DATEINT==05 || $DATEINT==10 || $DATEINT==15 || $DATEINT==59 || $DATEINT==20 || $DATEINT==25 || $DATEINT==30 || $DATEINT==35 || $DATEINT==40 || $DATEINT==45 || $DATEINT==50 || $DATEINT==55) {
            file_put_contents("${naam}.old", $local_file);
            if($DATEINT==00 || $DATEINT==30 || $DATEINT==15 ||$DATEINT==45 ||$DATEINT==59){
                if(!files_identical("${naam}","${naam}.old")) {
                    file_put_contents("history/${naam}.$DATETIME", $local_file);
                }

            }

        }
    }
}

Function 2 (compares the files):

function files_identical($file1, $file2) {
    if (md5_file($file1) == md5_file($file2)) {
        return TRUE;
    } elseif (md5_file($file1) != md5_file($file2)) {
        return FALSE;
    }
}

Somehow this does not work. Also, nothing in the logs (with verbose error logging on). The history folder is created sucessfully, the page is called every 5 seconds from multiple locations (while true; wget $url; sleep 5; done FTW). The other files ($naam.json) and ($naam.json.old) are created sucessfully, only the history folder stays empty. What am I missing here? ( @joepie91 )

Comments

  • NexusNexus Member
    edited September 2012

    Not trying to be rude or anything, but if you really need help, http://forums.phpfreaks.com/forum/13-php-coding-help/

    great community for learning/problems with php :)

  • NikkiNikki Member
    edited September 2012

    In your second function you are calling md5_file AGAIN if they don't match, use a simple else, or variables to store the md5 string (Or simply try file_get_contents on it and md5())

    Probably not the cause, but still good to do in production environments.

    Make sure you have write permissions to the directory, could be something simple :D

Sign In or Register to comment.