栏目树
使用Chokidar-CLI+PHP脚本,实现文件实时同步


  1. 安装node.js环境。
  2. 安装Chokidar-CLI
    npm install -g chokidar-cli

  3. 编辑一个PHP脚本,假设命名为rsync.php
    array_shift($argv);//当前脚本名,丢弃
    $event = array_shift($argv);
    $file = implode('', $argv);//当目录里有空格时,会被当成多个参数
    $time = date("Y-m-d H:i:s");
    switch ($event) {
        case 'add':
        case 'change':
            $bast_path = realpath(__DIR__.'/../www.5dphp.com/');//这里获得需要同步的源目录
            $to_path = 'W:\\www.5dphp.com';//假设将linux的目录映射到本机的W盘
            $file = str_replace($bast_path, '', $file);
            $cmd = sprintf("copy /Y \"%s\" \"%s\"",$bast_path.$file,  $to_path.$file);
            printf("[%s]执行同步文件%s=>",$time,$file);
            echo `$cmd`;//运行并将运行结果输出
            echo "\n";
        ;
        break;
        default:
            ;
        break;
    }

  4. 编辑一个.bat脚本,假设命名为auto.bat
    chokidar D:\project\www.5dphp.com\** -i ".**" -c "php D:\\project\\script\\rsync.php {event} {path}"

  5. 双击运行auto.bat