E
E
Egor2021-04-04 13:06:44
PHP
Egor, 2021-04-04 13:06:44

Is there a way to calculate the checksum for a folder?

Good afternoon.
There was a need to calculate the checksum for a folder (for example, MD5).
I couldn't find the answer on Google (maybe I didn't search well).
Is it even possible to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-04-04
@KenKup11

function getMd5DirHash(string $dir): string
{
    $array = [];
    $dir = realpath($dir); 
    $fileSPLObjects =  new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
    foreach($fileSPLObjects as $fullFileName => $fileSPLObject ) {
        if ($fileSPLObject->isFile()) {
            $array[] = $fullFileName;
        }
    }
    $md5 = array_map('md5_file', $array);

    return md5(implode('', $md5));
}

painted to make it clearer, can be improved
$array[] = md5_file($fullFileName);
#$md5 = array_map('md5_file', $array);

return md5(implode('', $array));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question