Guest User

Untitled

a guest
Feb 18th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1. function checksum($start = __DIR__, $filename = '.files.md5', $export = false) {
  2.     $unknown_map = array();
  3.     if (($dir = opendir($start)) !== false) {
  4.         $unknown_map = (function (RecursiveDirectoryIterator $dir) {
  5.             $all_files = array();
  6.             foreach (new RecursiveIteratorIterator($dir) as $name => $object) {
  7.                 $all_files[$object->getPathname()] = md5_file($object->getPathname());
  8.             }
  9.             return $all_files;
  10.         })(new RecursiveDirectoryIterator($start, FilesystemIterator::SKIP_DOTS));
  11.     }
  12.     if ($export === true) {
  13.         file_put_contents($filename, "", FILE_APPEND);
  14.         foreach($unknown_map as $path => $md5) {
  15.             file_put_contents($filename, "{$md5} {$path}\n", FILE_APPEND);
  16.         }
  17.         return true;
  18.     }
  19.     $known_map = array();
  20.     if (($file = fopen($filename, 'r')) !== false) {
  21.         while (($line = fgets($file)) !== false) {
  22.             preg_match_all("/([a-z0-9]{32})[\s](.*)[\r|\n]/u", $line, $result, PREG_SET_ORDER);
  23.             list( , $f_md5, $f_to_check) = $result[0]; $known_map[$f_to_check] = $f_md5;
  24.         }
  25.         fclose($file);
  26.     }
  27.     return array_diff($unknown_map, $known_map);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment