<?php

$folder1 = "C:/Users/Administrator/Documents/Visual Studio 2015/Projects/vb-china/vb-china/";
$folder2 = "Z:/vb-china/";

$subdirs = ['Content','Scripts','Views','Areas/Admin/Views'];


# 只需要设置上面的3项,下面的就不用设置了


recursiveDelete($folder2);  //每次开始前先清空从文件夹,此行可酌情屏蔽掉
//先做互通有无并初始化seri
$filenames1_new = get_filenames($folder1);
$filenames2_new = get_filenames($folder2);
$filenames_all_new = array_merge($filenames1_new, $filenames2_new);
$files_dict = [];
foreach ($filenames_all_new as $file) {
	if (!in_array($file, $filenames1_new)) {
		mycopy($folder2.$file, $folder1.$file);
	}
	if (!in_array($file, $filenames2_new)) {
		mycopy($folder1.$file, $folder2.$file);
	}
	$files_dict[$file] = md5_file($folder1.$file);
}




$i = 0;
while (true) {
	$i++;
	usleep(500000);

	foreach ($files_dict as $file => $md5) {   //对比文件MD5
		$file1_md5 = @md5_file($folder1.$file);
		if ($file1_md5 and $file1_md5 != $md5) {
			mycopy($folder1.$file, $folder2.$file);
			print_line("modify: ".$folder1.$file);
			$files_dict[$file] = md5_file($folder1.$file);
		} else {
			$file2_md5 = @md5_file($folder2.$file);
			if ($file2_md5 and $file2_md5 != $md5) {
				mycopy($folder2.$file, $folder1.$file);
				print_line("modify: ".$folder2.$file);
				$files_dict[$file] = md5_file($folder2.$file);
			}
		}
	}
	print_r('.');

	if ($i%10 == 0) {
		$filenames1_new = get_filenames($folder1);
		$filenames2_new = get_filenames($folder2);

		foreach (array_diff(array_keys($files_dict), $filenames1_new) as $deleted_file) {   //先清理已删除的文件,并更新记录
			unlink($folder2.$deleted_file);
			print_line("delete: ".$deleted_file);
			unset($files_dict[$deleted_file]);
		}
		foreach (array_diff($filenames1_new, array_keys($files_dict)) as $new_file) {   //再添加新文件,并更新记录
			mycopy($folder1.$new_file, $folder2.$new_file);
			print_line("create: ".$folder1.$new_file);
			$files_dict[$new_file] = md5_file($folder1.$new_file);
		}
		foreach (array_diff(array_keys($files_dict), $filenames2_new) as $deleted_file) {   //先清理已删除的文件,并更新记录
			unlink($folder1.$deleted_file);
			print_line("delete: ".$deleted_file);
			unset($files_dict[$deleted_file]);
		}
		foreach (array_diff($filenames2_new, array_keys($files_dict)) as $new_file) {   //再添加新文件,并更新记录
			mycopy($folder2.$new_file, $folder1.$new_file);
			print_line("create: ".$folder2.$new_file);
			$files_dict[$new_file] = md5_file($folder2.$new_file);
		}
		print_r('|');
	}
}






function get_filenames($folder)
{
	global $subdirs;
	$filenames = [];
	foreach ($subdirs as $dir) {
		$dir1 = $folder.'/'.$dir;
		foreach (walk($dir1.'/**.*') as $file) {
			$filenames[] = str_replace($folder, '', $file);
		}
	}
	return $filenames;
}

function walk($pattern){
	$files = glob($pattern);
	foreach(array_reverse(glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT)) as $dir){
		$files = array_merge(walk($dir.'/'.basename($pattern)), $files);
	}
	return $files;
}

function mycopy($s1,$s2) {
	$path = pathinfo($s2);
	if (!file_exists($path['dirname'])) {
		mkdir($path['dirname'], 0777, true);
	}
	if (!copy($s1,$s2)) {
		echo "copy failed \n";
	}
}

function print_line($str)
{
	print_r("\n\n");
	print_r(date("H:i:s").' '.$str);
	print_r("\n\n");
}

function recursiveDelete($str) {
	if (is_file($str)) {
		return @unlink($str);
	} elseif (is_dir($str)) {
		$scan = glob(rtrim($str,'/').'/*');
		foreach($scan as $index=>$path) {
			recursiveDelete($path);
		}
		return @rmdir($str);
	}
	return false;
}