I need to check if a file that existed in a directory on the server is the same file that was there say 5 minutes ago?
On a specific directory, I’m deleting and recreating a file with the same name every few minutes. A script is to access the file say every 2 mins. So at time t, the script checks the file and do stuffs. Then at time t+1, the file is deleted and recreated (same name but maybe different content). Then at time t+2, the script again looks for the file.
So in the second check , is there a way to check whether the file has been changed or not (without having to read the content)??
The first idea that comes to mind to determine whether a file has changed is to check its modification time, using the
filemtime()function.But I’m guessing this will not quite work, as you are deleting and re-creating the file regularly.
So, in your case, a very quick *(but not safe, of course)* first way is to **check the size of the file** : if it has changed, it is not the same file.
In PHP, the size of a file can be determined using the [**`filesize()`**][2] function.
Then, if the size has not changed, you’ll generally want to **calculate a hash of the file’s content** — which is generally either an [md5][3] or [sha1][4] hash.
In PHP, you’ll use one of the following functions, depending on which algorithm you want to work with :
sha1_file()md5_file()