使用方法:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
<?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());
同步類
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
/**
* 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;
* 目标目錄名
private $dest;
* 源目錄資料
private $src_data = 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;
* 同步
* @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';
}
}
}
* 傳回同步的檔案清單
public function getsync()
return $this->sync;
* 擷取目錄下的所有目錄和檔案
* @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