检查不同目录中的文件是否与原始文件相同

时间:2015-01-29 07:45:07

标签: php

我现在尝试过一些不太成功的事情。

到目前为止我尝试过的是:

echo sha1_file("development/index.php")." <- Dev<br>"; // 39e7851f050051abf91eb0791ae6a87084113dd9
echo sha1_file("customer/index.php")." <- Customer"; //17c64da656fae781dd404bbdc10fd2bcb58e12d9

echo filesize("development/index.php")." <- Dev<br>"; //369711 
echo filesize("customer/index.php")." <- Customer"; //363338

和这个

echo filemtime("development/index.php")." <- Dev<br>"; // 1422516938 
echo filemtime("customer/index.php")." <- Customer"; // 1422516943 

文件是相同的 - 我使用filezilla将一个文件从开发复制到了客户。

我认为sha1 _- / md5由于filetime不同而无效。

如何检查路径customer/index.php中的文件是否与development/index.php中的文件相同?

对于遇到同样问题的其他人:文件不是相同

解决此问题以确保文件完全相同:

copy('development/index.php', 'customer/index.php'); // copy file

echo sha1_file("development/index.php")." <- Dev<br>";
echo sha1_file("customer/index.php")." <- Cust<br>";

2 个答案:

答案 0 :(得分:2)

如果两个文件都有不同的名称和不同的filetime值,那么它们的哈希值自然会有所不同。如果您想知道两个文件的内容是否相同,您必须比较内容的哈希值,或者只是将内容作为一个整体进行比较:

$contentHash1 = sha1(file_get_contents($file1));
$contentHash2 = sha1(file_get_contents($file2));
echo 'Hash 1: ', $contentHash1, '<br>Hash 2: ', $contentHash2;

如果这些哈希值仍然不匹配,请在内容字符串上尝试trim(其中一个文件中的额外行很容易错过,并确保两个文件中的编码相同。

答案 1 :(得分:0)

我认为你应该尝试一些允许按内容进行比较的工具(例如总指挥官)

然后查找有什么不同之处,这会让你深入了解为什么你的sha方法不起作用,如果你想在php中比较文件,可能还有什么可做的。