Thursday, September 13, 2012

PHP, check if a file is modified using md5_file



To check if a file is modified in PHP,  we use md5 algorithm and PHP function md5_file() function
1)  First, we store the file for example " test.txt" md5 in  md5test.txt using PHP function   md5_file
<?php
    $md5file = md5_file("test.txt");
    file_put_contents("md5test.txt",$md5file);
  ?>

2)  As the time goes by, we can compare the md5 of the file to the md5 stored.
<?php
    $md5file = file_get_contents("md5test.txt");
    if (md5_file("test.txt") == $md5file)
    {
        echo "The file test.txt does not change.";
    }
    else
    {
        echo "The file test.txt has been changed.";
    }
   ?> 

No comments:

Post a Comment