Oihana PHP

filesAreEqual.php

Table of Contents

Functions

filesAreEqual()  : bool
Tells whether two files have identical contents.

Functions

filesAreEqual()

Tells whether two files have identical contents.

filesAreEqual(string $a, string $b[, string $algorithm = 'sha256' ]) : bool

The comparison is short-circuited for speed:

  1. if both paths resolve to the same file on disk, returns true without reading;
  2. if the file sizes differ, returns false without hashing;
  3. otherwise the files are compared by fileChecksum().
Parameters
$a : string

Path to the first file.

$b : string

Path to the second file.

$algorithm : string = 'sha256'

A hashing algorithm supported by hash_algos() (default: 'sha256').

Tags
throws
FileException

If either file is missing or unreadable, or the algorithm is unsupported.

author

Marc Alcaraz (ekameleon)

since
1.2.0
example
use function oihana\files\filesAreEqual;

if ( filesAreEqual( '/data/a.bin' , '/backup/a.bin' ) )
{
    // contents match — safe to deduplicate
}
Return values
bool

true if both files have identical contents, false otherwise.

On this page

Search results