Top Questions
Top Questions

Reputation: 1852

Check if file in different directory is the same as original

I´ve tried some things now with minor to no success.

What i've tried so far is this:

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

and

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

and this

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

The files are the same - I copied the one from development to customer with filezilla.

I think the sha1_-/md5 didn't work because of the different filetime.

How can I check for sure that the file in the path customer/index.php is the same as in development/index.php ?

For anyone else facing the same problem: The files were not identical!

Solving this problem to make sure the files are really identical:

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

Upvotes: 1

Views: 666

Answers (2)

Martin Vaš&#237;ček
Martin Vaš&#237;ček

Reputation: 16

I think you should try some tool that allows compare by content (for example total commander)

Then look for what are the differences and that will give you some insight why your sha method wasn't working and maybe what else to do if you want to compare files in php.

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

If both files have different names, and different filetime values, then naturally their hashes will be different. If you want to know whether or not the content of both files is identical, you'll have to compare the hashes of the content, or simply compare the content as a whole:

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

If these hashes still don't match, try trim on the content strings (an extra line in one of the files is easy to miss, and make sure the encoding is the same in both files.

Upvotes: 2

Related Questions