Last active
December 14, 2015 19:33
-
-
Save azihassan/fb984649e4ca96d1c67d to your computer and use it in GitHub Desktop.
Memory-friendly function that compares two files and returns a boolean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(compare_files('file1', 'file2')) | |
echo 'Files are similar', PHP_EOL; | |
else | |
echo 'Files are different', PHP_EOL; | |
function compare_files($a, $b, $buf_size = 1048576) | |
{ | |
if($a === $b) | |
return true; | |
if(filesize($a) != filesize($b)) | |
return false; | |
$fa = fopen($a, 'rb'); | |
$fb = fopen($b, 'rb'); | |
while(!feof($fa)) | |
{ | |
$buf = fread($fa, $buf_size); | |
if($buf != fread($fb, $buf_size)) | |
return false; | |
} | |
fclose($fa); | |
fclose($fb); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment