设为首页 收藏本站
查看: 711|回复: 0

[经验分享] 文件同步类SimFileSync.class.php

[复制链接]

尚未签到

发表于 2017-4-1 11:21:27 | 显示全部楼层 |阅读模式
  使用方法: 

<?php
require 'SimFileSync.class.php';
// 新建实例
$sync = new SimFileSync();
$src = "F:/www/simphp";
$dest = "F:/www/simphp_sae";
// 设置排除文件夹和文件名
$sync->set('exclude_dir_array', array(
'.svn',
'.settings'
))->set('exclude_file_array', array(
'.project',
'.buildpath'
));
// 同步
$sync->sync($src, $dest);
// 返回同步列表
print_r($sync->getSync());
  同步类

<?php
/**
* Sim, Simple library simplify our PHP development.
* 使用简单、简洁的类库,简化我们的PHP开发。
*
* @author 雨中歌者 http://weibo.com/esinger (新浪微博)
* @link http://blog.csdn.net/esinger (技术博客)
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* 文件同步类
* 主要功能:
* 1.把源文件夹内所有文件和子文件夹同步到目标文件夹
* 2.可以同步到多个文件夹
* 3.可以设置同步规则(正则或者数组),指定哪些文件和文件夹不进行同步
* 4.返回源文件夹、目标文件夹列表
* 5.返回同步的文件列表
*
* @author 雨中歌者
* @version 1.0
*/
class SimFileSync
{
/**
* 初始配置值
*
* @var array
*/
private $ini = array(
'exclude_dir_pattern' => '',
'exclude_file_pattern' => '',
'exclude_dir_array' => array(),
'exclude_file_array' => array()
);
/**
* 源目录名
*
* @var string
*/
private $src;
/**
* 目标目录名
*
* @var string
*/
private $dest;
/**
* 源目录数据
*
* @var array
*/
private $src_data = array();
/**
* 文件同步情况
*
* @var array
*/
private $sync = array();
/**
* 构造函数
*/
public function __construct()
{
}
/**
* 设置参数
* 1.$name为string,参数键名,$value为参数值,如 set('name','value')
* 2.$name为array,参数键值对数组,如 set(array('name'=>'value'))
*
* @access public
* @param string|array $name 参数键名或键值对数组
* @param mixed|null $value 参数值
* @return SimFileSync
*/
public function set($name, $value = null)
{
if (is_array($name)) {
$this->ini = array_merge($this->ini, $name);
} elseif (is_string($name)) {
$this->ini[$name] = $value;
}
return $this;
}
/**
* 同步
*
* @access public
* @param string $src 源文件目录
* @param string $dest 目标文件目录
* @return array
*/
public function sync($src, $dest)
{
$this->src = rtrim($src, '/\\') . '/';
$this->dest = rtrim($dest, '/\\') . '/';
$this->src_data = $this->getFile($src);
foreach ($this->src_data as $file => $type) {
$dest = str_replace($this->src, $this->dest, $file);
if ($type == 'dir' && !is_dir($dest)) {
// 目录不存在,创建目录
mkdir($dest, 0777, true);
$this->sync[$file] = 'mkdir';
} elseif ($type == 'file') {
if (!is_file($dest)) {
// 目标文件不存在,复制文件
$dir = dirname($dest);
is_dir($dir) or mkdir($dir, 0777, true);
copy($file, $dest);
$this->sync[$file] = 'newfile';
} else {
if (md5_file($file) != md5_file($dest)) {
// 目标文件存在,但修改时间不一样,覆盖文件
copy($file, $dest);
$this->sync[$file] = 'rewrite';
}
}
}
}
}
/**
* 返回同步的文件列表
*
* @access public
* @return array
*/
public function getSync()
{
return $this->sync;
}
/**
* 获取目录下的所有目录和文件
*
* @access public
* @param string $dirname
* @return array 不是目录或目录打开失败返回空数组
*/
public function getFile($dirname)
{
$dirname = rtrim($dirname, '/\\');
$ret = array();
if (is_dir($dirname)) {
if (($dh = @opendir($dirname)) !== false) {
while (false !== ($file = readdir($dh))) {
if ($file != "." && $file != "..") {
$path = $dirname . '/' . $file;
if (is_dir($path)) {
if (!$this->isExcluded($path, 'dir')) {
$ret[$path] = 'dir';
$ret = array_merge($ret, $this->getFile($path));
}
} else {
if (!$this->isExcluded($path, 'file')) {
$ret[$path] = 'file';
}
}
}
}
closedir($dh);
}
}
return $ret;
}
/**
* 是否被排除文件
*
* @access private
* @param string $filename 文件名
* @param boolean $type 目录或者文件(dir|file)
* @return boolean
*/
private function isExcluded($filename, $type)
{
$filename = basename($filename);
$pattern = $this->ini["exclude_{$type}_pattern"];
$array = $this->ini["exclude_{$type}_array"];
if ((!empty($pattern) && preg_match($pattern, $filename)) || in_array($filename, $array)) {
return true;
}
return false;
}
/**
* * 析构函数
*/
public function __destruct()
{
unset($this->ini);
}
}
// End of file SimFileSync.class.php

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-358627-1-1.html 上篇帖子: php 脱掉菜鸟的羽毛 下篇帖子: 改进型的PHP冒泡算法与传统的PHP冒泡算法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表