• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

刚开始有这个想法的时候找了很多资料。关于 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.\' [email protected]:/home/\'.$file);
                                break;
                            case \'WINNT\':
                                exec(\'PSCP.exe \'.$file.\' [email protected]:/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 上运行 出错了 还在找原因。

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
CI框架--核心文件之Model.php发布时间:2022-07-10
下一篇:
php编译安装报错make***[libphp5.la]Error1解决方法发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap