Skip to main content

If you just need to find out if two files are identical, comparing file hashes can be inefficient, especially on large files. There's no reason to read two whole files and do all the math if the second byte of each file is different. If you don't need to store the hash value for later use, there may not be a need to calculate the hash value just to compare files. This can be much faster.

/**
 * Check if two files are identical.
 *
 * If you just need to find out if two files are identical, comparing file
 * hashes can be inefficient, especially on large files.  There's no
 * reason to read two whole files and do all the math if the
 * second byte of each file is different.  If you don't need to
 * store the hash value for later use, there may not be a need to
 * calculate the hash value just to compare files.This can be much faster.
 *
 * @link http://www.php.net/manual/en/function.md5-file.php#94494
 *
 * @param string $fileOne
 * @param string $fileTwo
 * @return boolean
 */
function identical($fileOne, $fileTwo)
{
    if (filetype($fileOne) !== filetype($fileTwo)) return false;
    if (filesize($fileOne) !== filesize($fileTwo)) return false;

    if (! $fp1 = fopen($fileOne, 'rb')) return false;

    if (! $fp2 = fopen($fileTwo, 'rb'))
    {
        fclose($fp1);
        return false;
    }

    $same = true;

    while (! feof($fp1) and ! feof($fp2))
        if (fread($fp1, 4096) !== fread($fp2, 4096))
        {
            $same = false;
            break;
        }

    if (feof($fp1) !== feof($fp2)) $same = false;

    fclose($fp1);
    fclose($fp2);

    return $same;
}