Skip to content

Instantly share code, notes, and snippets.

@azihassan
Last active December 14, 2015 19:33
Show Gist options
  • Save azihassan/fb984649e4ca96d1c67d to your computer and use it in GitHub Desktop.
Save azihassan/fb984649e4ca96d1c67d to your computer and use it in GitHub Desktop.
Memory-friendly function that compares two files and returns a boolean
<?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