Comparing files in PHP

Posted under » PHP on 09 January 2023

Please read my article on hashing for an intro on encryption. You may also want to use MD5 but it is more taxing on the server.

Store the SHA-1 hash of "taik.txt" in a file:

<¿php
$sha1file = sha1_file("taik.txt");
file_put_contents("sha1file.txt",$sha1file);
?>

Best to touch "sha1file.txt" and make sure you or www-data is able to write to it.

Test if "taik.txt" has been changed (that is if the SHA-1 hash has been changed):

<¿php
$sha1file = file_get_contents("sha1file.txt");
if (sha1_file("taik.txt") == $sha1file)
  {
  echo "The file is ok.";
  }
else
  {
  echo "The file has been changed.";
  }
?>

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data