How to Check If Directory Contents Has Changed with PHP

How to check if directory contents has changed with PHP?

Uh. I'd simply store the md5 of a directory listing. If the contents change, the md5(directory-listing) will change. You might get the very occasional md5 clash, but I think that chance is tiny enough..

Alternatively, you could store a little file in that directory that contains the "last modified" date. But I'd go with md5.


PS. on second thought, seeing as how you're looking at performance (caching) requesting and hashing the directory listing might not be entirely optimal..

Use PHP to check if directory files have changed

I would try to make a hash of directory content, let's say concating the names of files (sorted) with some divider and making md5/sha1 hash. You'll need to store it in some way (in directory as text file or database).

On access you'll have to calculate actual hash of directory in the same way and compare it with the old (saved) one. Depending on result, you can take some actions...

How to detect changes in directories or file inside that directory using php

Here is my solution i have implemented. By using these function i am able to get a MD5 hash code and i can use this hash code to check if there is any changes in directory structure or in any file.

        $dir="PATH TO PRODUCT DIRECTORY";
public function directoryHash($dir, $item_name){
$dirStr=$this->directoryStruture($dir, $separator= DIRECTORY_SEPARATOR, $root='');
$strHash=md5(json_encode($dirStr));
return $strHash;
}

public function directoryStruture($dir, $separator = DIRECTORY_SEPARATOR, $root = '') {
$result = array();

$cdir = scandir($dir);
$result = array();
if ($root === '') {
$root = $dir;
}
$cdir = scandir($dir);
foreach ($cdir as $key => $value) {

if (!in_array($value, array(".", ".."))) {
$current = $dir . $separator . $value;
if (is_dir($current)) {
$result['items'][] = $value;
$result[$value] = $this->directoryStruture($current, $separator, $root);
} else {
$filePath = str_replace($root, '', $current);
$result[] = $filePath.filemtime($current);
}
}
}
return $result;
}

If there is any better approach. Please let me know.

Thanks,

M.

How to check if file hash has changed with php

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.



Related Topics



Leave a reply



Submit