Can I use file_get_contents() to compare two files?

后端 未结 7 1667
无人及你
无人及你 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:59
    • Memory: e.g. you have a 32 MB memory limit, and the files are 20 MB each. Unrecoverable fatal error while trying to allocate memory. This can be solved by checking the files by smaller parts.
    • Speed: string comparisons are not the fastest thing in the world, calculating a sha1 hash should be faster (if you want to be 110% sure, you can compare the files byte-by-byte when hash matches, but you'll rule out all the cases where content and hash change (99%+ cases))
    • Efficiency: do some preliminary checks - e.g. there's no point comparing two files if their size differs.
    0 讨论(0)
提交回复
热议问题