<?php
$folder1 = "C:/Users/Administrator/Documents/Visual Studio 2015/Projects/vb-china/vb-china/";
$folder2 = "Z:/vb-china/";
$subdirs = ['Content','Scripts','Views'];
# 只需要设置上面的3项,下面的就不用设置了
$files_dict_seri = __DIR__.'/files_dict.seri';
//先做互通有无并初始化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] = filesize($folder1.$file);
}
file_put_contents($files_dict_seri, serialize($files_dict));
$time1 = microtime(true);
foreach (range(1,1000) as $i) {
usleep(300000);
$files_dict = unserialize(file_get_contents($files_dict_seri));
foreach ($files_dict as $file => $size) { //不存在的删除,存在的对比文件大小
if (!is_file($folder1.$file)) {
unlink($folder2.$file);
print_line("delete: ".$folder1.$file);
unset($files_dict[$file]);
file_put_contents($files_dict_seri, serialize($files_dict));
} elseif (filesize($folder1.$file) != $size) {
mycopy($folder1.$file, $folder2.$file);
print_line("modify: ".$folder1.$file);
$files_dict[$file] = filesize($folder1.$file);
file_put_contents($files_dict_seri, serialize($files_dict));
} elseif (!is_file($folder2.$file)) {
unlink($folder1.$file);
print_line("delete: ".$folder2.$file);
unset($files_dict[$file]);
file_put_contents($files_dict_seri, serialize($files_dict));
} elseif (filesize($folder2.$file) != $size) {
mycopy($folder2.$file, $folder1.$file);
print_line("modify: ".$folder2.$file);
$files_dict[$file] = filesize($folder2.$file);
file_put_contents($files_dict_seri, serialize($files_dict));
}
}
print_r('.');
if ($i%10 == 0) {
$filenames1_new = get_filenames($folder1);
$filenames2_new = get_filenames($folder2);
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] = filesize($folder1.$new_file);
file_put_contents($files_dict_seri, serialize($files_dict));
}
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] = filesize($folder2.$new_file);
file_put_contents($files_dict_seri, serialize($files_dict));
}
print_r('|');
}
}
$time2 = microtime(true);
echo $time2 - $time1;
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");
print_r($str);
print_r("\n");
}