文件同步类SimFileSync.class.php

简介:

使用方法: 

Java代码   收藏代码
  1. <?php  
  2. require 'SimFileSync.class.php';  
  3.   
  4. // 新建实例  
  5. $sync = new SimFileSync();  
  6.   
  7. $src = "F:/www/simphp";  
  8. $dest = "F:/www/simphp_sae";  
  9.   
  10. // 设置排除文件夹和文件名  
  11. $sync->set('exclude_dir_array', array(  
  12.     '.svn',  
  13.     '.settings'  
  14. ))->set('exclude_file_array', array(  
  15.         '.project',  
  16.         '.buildpath'  
  17.     ));  
  18.   
  19. // 同步  
  20. $sync->sync($src, $dest);  
  21.   
  22. // 返回同步列表  
  23. print_r($sync->getSync());  

同步类

Java代码   收藏代码
  1. <?php  
  2. /** 
  3.  * Sim, Simple library simplify our PHP development. 
  4.  * 使用简单、简洁的类库,简化我们的PHP开发。 
  5.  * 
  6.  * @author 雨中歌者 http://weibo.com/esinger (新浪微博) 
  7.  * @link http://blog.csdn.net/esinger (技术博客) 
  8.  * @license http://www.apache.org/licenses/LICENSE-2.0 
  9.  */  
  10.   
  11. /** 
  12.  * 文件同步类 
  13.  * 主要功能: 
  14.  * 1.把源文件夹内所有文件和子文件夹同步到目标文件夹 
  15.  * 2.可以同步到多个文件夹 
  16.  * 3.可以设置同步规则(正则或者数组),指定哪些文件和文件夹不进行同步 
  17.  * 4.返回源文件夹、目标文件夹列表 
  18.  * 5.返回同步的文件列表 
  19.  * 
  20.  * @author 雨中歌者 
  21.  * @version 1.0 
  22.  */  
  23. class SimFileSync  
  24. {  
  25.     /** 
  26.      * 初始配置值 
  27.      * 
  28.      * @var array 
  29.      */  
  30.     private $ini = array(  
  31.         'exclude_dir_pattern' => '',  
  32.         'exclude_file_pattern' => '',  
  33.         'exclude_dir_array' => array(),  
  34.         'exclude_file_array' => array()  
  35.     );  
  36.   
  37.     /** 
  38.      * 源目录名 
  39.      * 
  40.      * @var string 
  41.      */  
  42.     private $src;  
  43.   
  44.     /** 
  45.      * 目标目录名 
  46.      * 
  47.      * @var string 
  48.      */  
  49.     private $dest;  
  50.   
  51.     /** 
  52.      * 源目录数据 
  53.      * 
  54.      * @var array 
  55.      */  
  56.     private $src_data = array();  
  57.   
  58.     /** 
  59.      * 文件同步情况 
  60.      * 
  61.      * @var array 
  62.      */  
  63.     private $sync = array();  
  64.   
  65.     /** 
  66.      * 构造函数 
  67.      */  
  68.     public function __construct()  
  69.     {  
  70.     }  
  71.   
  72.     /** 
  73.      * 设置参数 
  74.      * 1.$name为string,参数键名,$value为参数值,如 set('name','value') 
  75.      * 2.$name为array,参数键值对数组,如 set(array('name'=>'value')) 
  76.      * 
  77.      * @access public 
  78.      * @param string|array $name 参数键名或键值对数组 
  79.      * @param mixed|null $value 参数值 
  80.      * @return SimFileSync 
  81.      */  
  82.     public function set($name, $value = null)  
  83.     {  
  84.         if (is_array($name)) {  
  85.             $this->ini = array_merge($this->ini, $name);  
  86.         } elseif (is_string($name)) {  
  87.             $this->ini[$name] = $value;  
  88.         }  
  89.         return $this;  
  90.     }  
  91.   
  92.     /** 
  93.      * 同步 
  94.      * 
  95.      * @access public 
  96.      * @param string $src 源文件目录 
  97.      * @param string $dest 目标文件目录 
  98.      * @return array 
  99.      */  
  100.     public function sync($src, $dest)  
  101.     {  
  102.         $this->src = rtrim($src, '/\\') . '/';  
  103.         $this->dest = rtrim($dest, '/\\') . '/';  
  104.         $this->src_data = $this->getFile($src);  
  105.         foreach ($this->src_data as $file => $type) {  
  106.             $dest = str_replace($this->src, $this->dest, $file);  
  107.             if ($type == 'dir' && !is_dir($dest)) {  
  108.                 // 目录不存在,创建目录  
  109.                 mkdir($dest, 0777true);  
  110.                 $this->sync[$file] = 'mkdir';  
  111.             } elseif ($type == 'file') {  
  112.                 if (!is_file($dest)) {  
  113.                     // 目标文件不存在,复制文件  
  114.                     $dir = dirname($dest);  
  115.                     is_dir($dir) or mkdir($dir, 0777true);  
  116.                     copy($file, $dest);  
  117.                     $this->sync[$file] = 'newfile';  
  118.                 } else {  
  119.                     if (md5_file($file) != md5_file($dest)) {  
  120.                         // 目标文件存在,但修改时间不一样,覆盖文件  
  121.                         copy($file, $dest);  
  122.                         $this->sync[$file] = 'rewrite';  
  123.                     }  
  124.                 }  
  125.             }  
  126.         }  
  127.     }  
  128.   
  129.     /** 
  130.      * 返回同步的文件列表 
  131.      * 
  132.      * @access public 
  133.      * @return array 
  134.      */  
  135.     public function getSync()  
  136.     {  
  137.         return $this->sync;  
  138.     }  
  139.   
  140.     /** 
  141.      * 获取目录下的所有目录和文件 
  142.      * 
  143.      * @access public 
  144.      * @param string $dirname 
  145.      * @return array 不是目录或目录打开失败返回空数组 
  146.      */  
  147.     public function getFile($dirname)  
  148.     {  
  149.         $dirname = rtrim($dirname, '/\\');  
  150.         $ret = array();  
  151.         if (is_dir($dirname)) {  
  152.             if (($dh = @opendir($dirname)) !== false) {  
  153.                 while (false !== ($file = readdir($dh))) {  
  154.                     if ($file != "." && $file != "..") {  
  155.                         $path = $dirname . '/' . $file;  
  156.                         if (is_dir($path)) {  
  157.                             if (!$this->isExcluded($path, 'dir')) {  
  158.                                 $ret[$path] = 'dir';  
  159.                                 $ret = array_merge($ret, $this->getFile($path));  
  160.                             }  
  161.                         } else {  
  162.                             if (!$this->isExcluded($path, 'file')) {  
  163.                                 $ret[$path] = 'file';  
  164.                             }  
  165.                         }  
  166.                     }  
  167.                 }  
  168.                 closedir($dh);  
  169.             }  
  170.         }  
  171.         return $ret;  
  172.     }  
  173.   
  174.     /** 
  175.      * 是否被排除文件 
  176.      * 
  177.      * @access private 
  178.      * @param string $filename 文件名 
  179.      * @param boolean $type 目录或者文件(dir|file) 
  180.      * @return boolean 
  181.      */  
  182.     private function isExcluded($filename, $type)  
  183.     {  
  184.         $filename = basename($filename);  
  185.         $pattern = $this->ini["exclude_{$type}_pattern"];  
  186.         $array = $this->ini["exclude_{$type}_array"];  
  187.         if ((!empty($pattern) && preg_match($pattern, $filename)) || in_array($filename, $array)) {  
  188.             return true;  
  189.         }  
  190.         return false;  
  191.     }  
  192.   
  193.     /** 
  194.      * * 析构函数 
  195.      */  
  196.     public function __destruct()  
  197.     {  
  198.         unset($this->ini);  
  199.     }  
  200. }  
  201. // End of file SimFileSync.class.php  

 

相关文章
|
8月前
|
XML 前端开发 JavaScript
PHP多文件压缩并分片下载文件详细介绍,附上完整代码
有时我们经常需要压缩下载多个文件,我这里主要采用在fastadmin框架中添加了一个表格自定义按钮,并为按钮绑定相应的事件来实现。
160 0
|
11月前
|
PHP
PHP 下载文件读取文件并下载
PHP 下载文件读取文件并下载
73 0
|
PHP
PHP实战:guzzlehttp/guzzle下载网络文件到本地
PHP实战:guzzlehttp/guzzle下载网络文件到本地
580 0
|
机器学习/深度学习 移动开发 PHP
|
PHP Web App开发
PHP Header下载文件在IE文件名中文乱码问题
解决PHP Header下载文件在IE文件名中文乱码有两种常见的,一种是是把页面编码改成utf8,另一种是对中文url进入urlencode编码,根据UA检测,区别下载,就可以解决了 $filename = "中文.
1246 0
|
PHP
通过php下载文件并重命名
$filename = dirname(__FILE__) . '/oldfilename.jpg';$out_filename = 'newfilename.jpg';if( ! file_exists($filename)){  echo 'Not Found' .
1020 0
|
PHP
PHP实现远程下载文件到本地
PHP实现远程下载文件到本地 投稿:hebedich 字体:[增加 减小] 类型:转载   经常写采集器发布接口需要使用到远程附件的功能,所以自己写了一个PHP远程下载文件到本地的函数,一般情况下已经够用了,如果服务器支持CURL函数,程序则会优先选择CURL,有需要的小伙伴可以参考下。
1213 0