I have an xml file with database information that should be loaded when the script is installed or when the content of the file changes. Can I use md5_file() on the xml file and then store the value in the db and compare it to the file's hash each time the script is run? Does it have any drawbacks and are there any other methods that are faster/simpler?
-
Yes, you can use a hash, or simply check the modified time of the file. You could just have typed you question in a search engine... – Green Black Feb 04 '13 at 23:46
-
4I just typed it into a search engine and I ended up here. A totally legit question and this is the best place for a discussion, imo. – Octopus Jan 30 '14 at 20:11
5 Answers
It depends on what you mean by "changed". If you require that the contents actually be modified, then checking filemtime
is not enough - it's a fantastic first step, and should be used first, but it is not sufficient on its own.
Conbine the filemtime
with a hash of the file's contents (such as md5_file
) and it will work efficiently.

- 320,036
- 81
- 464
- 592
-
1Isn't just using md5_file enough then? I'm only interested in whether or not the content is different. If it's overwritten with the same content or if the filename changes is not important. – John Feb 05 '13 at 00:01
-
3Well, yes. However, it is costly. The `filemtime` check is intended to act as a much more efficient lookup. Then, only if the modification time has changed will it perform the expensive `md5_file` call. – Niet the Dark Absol Feb 05 '13 at 00:08
-
1But the only time the filetime is different but the hash isn't, is when it has been overwritten with the same information or when the filename has changed, right? – John Feb 05 '13 at 00:24
-
1
-
Files are not only the bytes, a md5 doesn't help you when extended attributes change. I hope one day people will always remember this and one time implement their software so that extended attributes become usable. – Lothar Apr 06 '21 at 00:05
Your best bet would be to store the file mod time and compare it to a file mod time you check in the future.
if(filemtime('myfile.txt') > $result_set['filemtime']) {
// file was modified
}
I have hope that you can do the databasing yourself.

- 2,697
- 15
- 27
Perhaps you could use PHP's filemtime()
function. Simply put, this function gets file modification time for a specified file.
This function returns a unix time stamp, so you'll need to save the last known modification time somewhere in order to compare it to the new value.
$modifiedTs = filemtime($filename);
if ($modifiedTs != $lastModificationTs){
echo "$filename was modified!";
}

- 47,311
- 12
- 103
- 131
-
-
2@cup_of - that would be the value from the last time you called `filemtime`. When you don't have a modification time, you can take the current time stamp - then when you call `filemtime` again and compare it to the previous value, it will be different and you'll know that the file has changed. – Lix Jul 25 '18 at 21:06
I know that I am going back in time here, but the question helped me so I want to share what I did with it, so while this is not an answer to the question I will make it a community wiki so it can be improved (I'm no expert).
This was part of a much larger class so I have stripped out the rest and provided just the applicable code and turned it into an easy to follow example using a very common file:
class ApplicationLogger {
private $logfile, $path;
public function __construct() {
$this->logfile = 'error.log';
$this->path = '/var/log/apache2/';
}
public function logState() {
$files = array(
'target' => $this->path . $this->logfile,
'lastmod' => $this->path . $this->logfile . '_lastmod'
);
if (file_exists($files['lastmod']) && file_exists($files['target'])) {
$lfh = fopen($files['lastmod'], 'r');
while (!feof($lfh)) {
$lines[] = fgets($lfh);
}
fclose($lfh);
}
$modified = false;
/**
* check if we have a matching hash.
*/
if (isset($lines) && filemtime($files['target']) != $lines[0]) { // mod time mismatch
if (md5_file($files['target']) != $lines[1]) { // content modified
$modified = true;
}
}
/**
* update or create the lastmod file
*/
if (!file_exists($files['lastmod']) || $modified) {
$current_mod = filemtime($files['target']) . "\n" . md5_file($files['target']);
file_put_contents($files['lastmod'], $current_mod);
$modified = true;
}
return $modified;
}
}
Usage is straight forward:
$mod = new ApplicationLogger();
if ($mod->logState()) {
// changed do something
}
Adapt it to your needs, improve it however you see fit. I hope that you will contribute your adaptations by editing this CW.

- 930
- 7
- 16
The simpliest way is to compare the file_date - you can use filemtime
for this

- 15,377
- 4
- 35
- 52