I want to compare two remote files in php code.Both file behind CDN. Please let me know if any one have any idea how to do this.
Asked
Active
Viewed 372 times
0
-
what you mean by compare? whether the file content is identical? or compare line by line? – user158 Oct 18 '19 at 04:31
-
Yes, i want to check both files have same content or not. – Thath Singh Oct 18 '19 at 04:33
1 Answers
1
You can compare checksums of the two files to see whether they contain the same content. You can use hash_file for generating checksums.
Example:
$file1 = // path to file 1
$file2 = // path to file 2
$hash_file1 = hash_file("sha256", $file1)
$hash_file2 = hash_file("sha256", $file2)
if ($hash_file1 != $hash_file2){
echo "File content does not match!"
}
Reference: https://www.php.net/manual/en/function.hash-file.php

user158
- 12,852
- 7
- 62
- 94
-
This is not working with different url of same file. Its working if i am pass both file url same. I don't know why. – Thath Singh Oct 18 '19 at 05:26
-
My both urls as :-- 1) https://securepubads.g.doubleclick.net/tag/js/gpt.js 2) https://static.indianexpress.com/poc/gpt.js BTW :- Both files have same content – Thath Singh Oct 18 '19 at 05:27
-
I checked myself files you linked are not same, so checksums do not match. – user158 Oct 18 '19 at 05:32
-
One file is own my server and other is on third party . I can't put both file on my server. – Thath Singh Oct 18 '19 at 05:32
-
Can you let me know the difference because i just copied one file into another. – Thath Singh Oct 18 '19 at 05:34
-
-
If i put both file on my server then also it is working with different urls. – Thath Singh Oct 18 '19 at 05:49
-
there is difference is file size, one is 47, 166 other one is 47, 162. so there is some whitespace added. this probably your text editor is adding whitespace on save – user158 Oct 18 '19 at 05:52
-
-
@ThathSingh what is the text editor, you used to copy and save the file? – user158 Oct 18 '19 at 06:13
-
-
chekout: [removing trailing spaces](https://stackoverflow.com/questions/12297169/sublime-text-2-trim-trailing-white-space-on-demand). by the way since the my answer answer your question, please upvote and mark as correct. – user158 Oct 18 '19 at 07:38
-
-
-
I write my file via php code not copy paste file manual. I am glade now it is working fine.Thanks once again. – Thath Singh Oct 18 '19 at 09:08
-
additional information for you: sometimes checksums can collide, but it is very rare. you may find [this](https://crypto.stackexchange.com/questions/47809/why-havent-any-sha-256-collisions-been-found-yet) interesting. In general checksums/hashes is security technique used to check whether a file have been tampered or not. – user158 Oct 18 '19 at 09:15
-
Thanks for [this](https://crypto.stackexchange.com/questions/47809/why-havent-any-sha-256-collisions-been-found-yet) information. – Thath Singh Oct 18 '19 at 09:34