<?php
function FS($dir){
//echo "scan $dir\r\n";
$res_tmp = array();
$files_tmp = scandir($dir);
foreach ($files_tmp as $filename){
if (is_dir($dir.'\\'.$filename)&&($filename!='.')&&($filename!='..'))
$res_tmp[$filename] = FS($dir.'\\'.$filename);
}
foreach ($files_tmp as $filename){
if (is_file($dir.'\\'.$filename)) $res_tmp[$filename]=array(
'md5' => md5_file($dir.'\\'.$filename),
'size' => filesize($dir.'\\'.$filename)
);
}
return $res_tmp;
}
function diff($ar1,$ar2){
$diff = array();
$diff['+'] = array();
$diff['-'] = array();
$diff['*'] = array();
foreach ($ar1 as $k => $v){
if (is_array($v)){
if( !isset($ar2[$k]) )
$diff['-'][$k] = $v;
else if ( !is_array($ar2[$k]) )
$diff['*'][$k] = array('old' => $v, 'new' => $ar2[$k]);
else {
$new_diff = diff($v, $ar2[$k]);
if( !empty($new_diff['+']) )
$diff['+'][$k] = $new_diff['+'];
if( !empty($new_diff['-']) )
$diff['-'][$k] = $new_diff['-'];
if( !empty($new_diff['*']) )
$diff['*'][$k] = $new_diff['*'];
}
}else
if( !array_key_exists($k,$ar2) )
$diff['-'][$k] = $v;
else if ( $ar2[$k] !== $v )
$diff['*'][$k] = array('old' => $v, 'new' => $ar2[$k]);
}
foreach ($ar2 as $k => $v){
if( !array_key_exists($k,$ar1) )
$diff['+'][$k] = $v;
}
return $diff;
}
$files = array();
$files = FS('\\\\server\\Share\\Каталог');
$res = fopen(date("Y-m-d-H-i-s").'.json','a+');
fwrite($res,json_encode($files));
fclose($res);
$reports = glob("*.json");
rsort($reports);
if (isset($reports[1])){
$content1 = file_get_contents($reports[0]);
$content1 = json_decode($content1, true);
$content2 = file_get_contents($reports[1]);
$content2 = json_decode($content2, true);
$res = fopen($reports[0].'-'.$reports[1].'-diff.txt','w+');
$diff = diff($content2,$content1);
fwrite($res,print_r($diff,true));
fclose($res);
echo "look report in file ".$reports[0].'-'.$reports[1].'-diff.txt';
}
?>