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


Check if file in different directory is the same as original

我现在尝试了一些事情,但收效甚微,甚至没有成功。

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

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>";

如果两个文件具有不同的名称和不同的filetime值,那么它们的哈希值自然会有所不同。如果你想知道两个文件的内容是否相同,你必须比较内容的哈希值,或者简单地比较整个内容:

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

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

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

然后寻找有什么差异,这将使您了解为什么您的sha方法不起作用,以及如果您想在php中比较文件,也许还能做什么。