• php 监控文件变化 并上传到服务器


    最近有个思路。想要监控本地文件变化 并上传到指定服务器。

    刚开始有这个想法的时候找了很多资料。关于 windows 和linux 上怎么监控文件变化的

    最后选择了 比较MD5值的方法。

    我也写了一段 但是也大同小异。 于是 其中借用了别人的代码  

    他的github 是 https://github.com/tansuo1989/mydemo/blob/master/php-demo/file_watch.php

    话不多说 上代码

    <?php
    /**
     * Created by PhpStorm.
     * User: eddie
     * Date: 2018/3/5
     * Time: 10:37
     */
    
    class FileWatch
    {
    
        protected $all = array();
    
        public function __construct($dir)
        {
            $this->watch($dir);
        }
    
        //子类中重写这个方法
        public function run($file)
        {
        }
    
        protected function all_file($dir)
        {
            if (is_file($dir)) {
                $this->all[$dir] = md5_file($dir);
                return $this->all;
            }
            if (is_dir($dir)) {
                $open = opendir($dir);
                while (($file = readdir($open)) !== false) {
                    if ($file != "." && $file != "..") {
                        $f = $dir . "/" . $file;
                        if (is_file($f)) {
                            $this->all[$f] = md5_file($f);
                        } elseif (is_dir($f)) {
                            $this->all_file($f);
                        }
    
                    }
                }
            }
            return $this->all;
        }
    
        public function watch($dir)
        {
            $this->all = array();
            $old = $this->all_file($dir);
            while (true) {
                sleep(2);
                $this->all = array();
                $new = $this->all_file($dir);
                $re = array_diff($new, $old);
                $del = array_diff_key($old, $new);
                $re = array_merge($re, $del);
                if ($re) {
                    $this->all = array();
                    $old = $this->all_file($dir);
                    $file = array_keys($re);
                    $this->run($file[0]);
                }
            }
        }
    }//endclass
    //使用方法
    class mywatch extends FileWatch
    {
        public function run($file)
        {
            if(!empty($file)) {
                echo "new file or file has been changed with ".$file.PHP_EOL;
                echo "please choose 1 or 0 to upload files or skip ".PHP_EOL;
                $fileName = explode('/', $file);
    
                while (true) {
                    $a = trim(fgets(STDIN));
                    if(ctype_digit($a)) {
                        echo $a.PHP_EOL;
                        if($a == 1) {
                            //上传文件
                            echo "you choose upload files ".PHP_EOL;
                            // 判断系统
                            switch (PHP_OS) {
                                //linux 需要用scp 命令
                                case 'Linux':
                                    exec('scp '.$file.' root@192.168.1.21:/home/'.$file);
                                    break;
                                case 'WINNT':
                                    exec('PSCP.exe '.$file.' root@192.168.1.21:/home/'.end($fileName));
                                    break;
                            }
                            break;
                        } else {
                            //不管 跳过
                            echo "you choose skip ".PHP_EOL;
                            break;
                        }
                    } else {
                        echo 'please enter 0 or 1'.PHP_EOL;
                    }
                }
            } else {
                echo "no files has created and no files has been changed".PHP_EOL;
            }
        }
    }
    echo 'Your System is '.PHP_OS.PHP_EOL;
    echo "Welcome to use fileWatch System".PHP_EOL;
    $watch = new mywatch("./");
    

      这段代码 在linux 上运行是没有报错 但是在windows 上运行 出错了 还在找原因。

  • 相关阅读:
    字符串匹配算法
    C#中窗体的位置和大小
    关于C#值类型,引用类型,值传递,引用传递(转载)
    ArcMap中设置.mxd相对路径
    统计学上的知识
    .NET 数学实现资料(ZZ)
    牛腩新闻系统学习笔记06讲 编写SQLHelper
    DropDownList 控件不能触发SelectedIndexChanged 事件的另一个原因
    牛腩新闻视频 03讲 数据库设计的心得 如何建立外键sql2008的数据库关系图功能
    使用sql server management studio 2008 连接数据库,无法查看数据库,提示 无法为该请求检索数据 错误916
  • 原文地址:https://www.cnblogs.com/eddief/p/8508316.html
Copyright © 2020-2023  润新知