Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file has changed?

Tags:

php

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?

like image 483
John Avatar asked Nov 28 '22 08:11

John


2 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.

like image 199
Niet the Dark Absol Avatar answered Dec 07 '22 23:12

Niet the Dark Absol


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.

like image 43
David Harris Avatar answered Dec 07 '22 22:12

David Harris