Can I use file_get_contents() to compare two files?

后端 未结 7 1707
无人及你
无人及你 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:49

    Use sha1_file() instead. It's faster and works fine if you just need to see whether the files differ. If the files are large, comparing the whole strings to each other can be very heavy. As sha1_file() returns an 40 character representation of the file, comparing files will be very fast.

    You can also consider other methods like comparing filemtime or filesize, but this will give you guaranteed results even if there's just one bit that's changed.

提交回复
热议问题