1

I have a php script that saves the file names of every file in directory to text file. That text file is then hashed. Here are the lines in my script:

$allnames = implode(",", $fileslist); file_put_contents($post_dir . "/md5file.txt",$allnames); $md5file = md5_file($post_dir . "/md5file.txt"); echo $md5file; 

If a new file is uploaded, removed, or a file name is changed then the hash will change. I need a way to check and see if that hash has changed or not.

5
  • 1
    Then save the hash and check against it? Commented Dec 19, 2013 at 22:07
  • File name changes alone shouldn't change the md5, as that's part of the filesystem entry rather than the file itself. Commented Dec 19, 2013 at 22:07
  • @Powerlord: in this specific case, the md5 will change since the file contents will change with the change in the name of other files. Commented Dec 19, 2013 at 22:08
  • You probably could check it against using strstr or strpos or any other piece of code (regex maybe) to find an exact match of a string. However, this would probably mean making a copy of the same file (at the same time) and match against it. Commented Dec 19, 2013 at 22:12
  • 1
    @Stoic Oh derp, didn't realize he was checking the md5 of the file that contained the list of files... I thought he was doing an md5 of the files themselves. Commented Dec 19, 2013 at 22:12

2 Answers 2

2

A dirty way (without using database) would be:

$allnames = implode(",", $fileslist); file_put_contents($post_dir . "/md5file.txt",$allnames); $md5file = md5_file($post_dir . "/md5file.txt"); $last_hash = file_get_contents($post_dir . "/last_hash.txt"); if ($md5file != $last_hash) { // save the new hash file_out_contents($post_dir . "/last_hash.txt", $md5file); // some file may have been added, removed or rename // do something.. } else { // no files has been added, removed or renamed, since last change. // do something.. } 

Basically, I am storing the current hash to a particular file, and reading this file on the next iteration. If the hash has changed, I store the new hash to this file for comparison later on, and voila!

Note that, the same can be performed over database, if you are using one.

Sign up to request clarification or add additional context in comments.

Comments

0

2 possibilities without a DB:

  • make a second file and save the last filename in it and compare against it.
  • include the file size into the filename or head of file and check if the file size has changed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.