Can I use file_get_contents() to compare two files?

后端 未结 7 1699
无人及你
无人及你 2020-12-03 03:09

I want to synchronize two directories. And I use

file_get_contents($source) === file_get_contents($dest)

to compare two files. Is there an

7条回答
  •  暖寄归人
    2020-12-03 03:39

    Check first for the obvious:

    1. Compare size
    2. Compare file type (mime-type).
    3. Compare content.

    (add comparison of date, file name and other metadata to this obvious list if those are also not supposed to be similar).

    When comparing content hashing sounds not very efficient like @Oli says in his comment. If the files are different they most likely will be different already in the beginning. Calculating a hash of two 50 Mb files and then comparing the hash sounds like a waste of time if the second bit is already different...

    Check this post on php.net. Looks very similar to that of @Svish but it also compares file mime-type. A smart addition if you ask me.

提交回复
热议问题