我可以使用file_get_contents()比较两个文件吗? [英] Can I use file_get_contents() to compare two files?
本文介绍了我可以使用file_get_contents()比较两个文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想同步两个目录.我用
I want to synchronize two directories. And I use
file_get_contents($source) === file_get_contents($dest)
比较两个文件.这样做有什么问题吗?
to compare two files. Is there any problem to do this?
推荐答案
我宁愿做这样的事情:
function files_are_equal($a, $b)
{
// Check if filesize is different
if(filesize($a) !== filesize($b))
return false;
// Check if content is different
$ah = fopen($a, 'rb');
$bh = fopen($b, 'rb');
$result = true;
while(!feof($ah))
{
if(fread($ah, 8192) != fread($bh, 8192))
{
$result = false;
break;
}
}
fclose($ah);
fclose($bh);
return $result;
}
这将检查文件大小是否相同,并逐步检查文件大小.
This checks if the filesize is the same, and if it is it goes through the file step by step.
- 在某些情况下,检查修改后的时间检查可能是一种快速的方法,但是除了文件在不同时间被修改之外,它并没有告诉您其他任何信息.它们可能仍具有相同的内容.
- 使用sha1或md5可能是一个好主意,但这需要遍历整个文件来创建该哈希.如果此哈希是可以在以后存储和使用的东西,则可能是另一回事了,但是是的...
这篇关于我可以使用file_get_contents()比较两个文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文