开发者

How to check if a file is the same file as before

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开发者_StackOverflow中文版 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() 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 or sha1 hash.

In PHP, you'll use one of the following functions, depending on which algorithm you want to work with :

  • sha1_file()
  • md5_file()


You can calculate a "fingerprint" of the file using the md5_file() function, which will indicate if the file has been altered.

e.g.

<?php

$file1_fp = md5_file($fname1);
$file2_fp = md5_file($fname2);

if ($file1_fp === $file2_fp )
{

  //do something..
}

?>

see: http://php.net/manual/en/function.md5-file.php


From PHP, you can use the function filemtime to get the last-modified timestamp for your file.

Be careful!

The return value of this function is cached, and calling it multiple times from the same script will result in the same value being returned even if the file has changed in the meantime! To work around this, you need to call clearstatcache before calling filemtime.

If you are calling filemtime once in every script (and then the script terminates), this does not affect you and you don't need to do clearstatcache.


Use filemtime to check the date the file was last modified. http://php.net/manual/en/function.filemtime.php


If you could trust the timestamp, use the File's timestamp to differentiate it, otherwise, use MD5 digest etc or do a bit to bit comparison.


I'd check whether the checksum of the file has changed. This one-liner calculates the MD5 hash:

md5(file_get_contents('/path/to/file'))

As an alternative, you could (if the OS and FS supports this) rely on file creation datetime.


Check the fileinode(). If the file was deleted and recreated the inode will be different even if the file will get identical content.


Compare the calculated MD5 - this should in dicate any changes to the file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜