<?php
/**
* 目录复制
* @author MoXie SysTem128@GMail.Com
*
*/
class Direr
{
public $folder; #目录集合
public $filePath ; #文件集合
/**
* 装载目录和文件信息
*
* @param unknown_type $folder
* @param unknown_type $sufFix
* @return unknown
*/
public function setupFolder($folder = null,$sufFix = 'html')
{
$folderDirList = array();
$filePathList = array();
settype($folder,'string');
if (is_dir($folder))
{
$folderList = glob($folder.'*');
$tplfileList = glob($folder.'*.'.$sufFix);
foreach ($tplfileList as $key => $value )
{
#如果需要对文件名称进行过滤 则在此进行
$filePath = $value;
if (is_file($value))
{
$filePathList[] = $filePath;
}
}
$this->insFilePath($filePathList);
$filePathList = null;
foreach ($folderList as $key => $value )
{
$folderDir = $value.'/';
if (is_dir($folderDir))
{
$folderDirList[] = $folderDir;
$this->setupFolder($folderDir,$sufFix);
}
}
$this->insFolder($folderDirList);
$folderDirList = null;
}
$this->insFolder($folderDirList);
return $folderDirList;
}
/**
* 推入目录信息
*
* @param unknown_type $folder
* @return unknown
*/
public function insFolder($folder = null)
{
$returnInfo = false;
if (is_array($folder) && !empty($folder))
{
$folderOld = $this->folder;
settype($folder,'array');
settype($folderOld,'array');
$this->folder = array_merge_recursive($folder,$folderOld);
$returnInfo = true;
}
return $returnInfo;
}
/**
* 获取目录信息
*
* @return unknown
*/
public function getFolder()
{
return $this->folder ;
}
/**
* 推入文件信息
*
* @param unknown_type $filePath
* @return unknown
*/
public function insFilePath($filePath = null)
{
$returnInfo = false;
if (is_array($filePath) && !empty($filePath))
{
$filePathOld = $this->filePath;
settype($filePathOld,'array');
$this->filePath = array_merge_recursive($filePath,$filePathOld);
$returnInfo = true;
}
return $returnInfo;
}
/**
* 获取文件信息
*
* @return unknown
*/
public function getFilePath()
{
return $this->filePath ;
}
public function creatFolder($folderList = null)
{
try {
foreach ($folderList as $folder)
{
if (!is_dir($folder))
{
mkdir($folder, 0700);
}
}
return true;
}
catch (Exception $e)
{
return false;
}
}
/**
* 复制文件
*
* @param unknown_type $sourceFilePathList
* @param unknown_type $targetFilePathList
* @return unknown
*/
public function copyFile($sourceFilePathList = null,$targetFilePathList = null)
{
try {
foreach ($targetFilePathList as $key => $value)
{
if (isset($sourceFilePathList[$key]))
{
$sourceFilePath = $sourceFilePathList[$key];
if (!is_file($value) && is_file($sourceFilePath))
{
copy($sourceFilePath,$value);
}
}
}
return true;
}
catch (Exception $e)
{
return false;
}
}
/**
* 替取目标文件夹名称列表
*
* @param unknown_type $item
* @param unknown_type $key
* @param unknown_type $replaceMent
*/
public function changeFolder(&$item,$key,$replaceMent)
{
$item = str_ireplace($replaceMent['source'],$replaceMent['target'],$item);
}
/**
* 复制目录所有目录与文件
*
* @param unknown_type $sourceFolder
* @param unknown_type $targetFolder
* @param unknown_type $sufFix
* @return unknown
*/
public function copyFolder($sourceFolder = null,$targetFolder = null,$sufFix = null)
{
$returnInfo = false;
do{
if(!is_dir($sourceFolder) || !is_dir($targetFolder))
{
break;
}
$this->setupFolder($sourceFolder,$sufFix);
$sourceFolderList = $this->getFolder();
$sourceFilepathList = $this->getFilePath();
$replaceMent['source'] = $sourceFolder;
$replaceMent['target'] = $targetFolder;
if (empty($sourceFolderList) && empty($sourceFilepathList))
{
break;
}
if (!empty($sourceFolderList))
{
$targetFolder = $sourceFolderList;
array_walk($targetFolder,array($this,'changeFolder'),$replaceMent);
if (!$this->creatFolder($targetFolder))
{
break;
}
}
if (empty($sourceFilepathList))
{
break;
}
$targetFilePath = $sourceFilepathList;
array_walk($targetFilePath,array($this,'changeFolder'),$replaceMent);
if (!$this->copyFile($sourceFilepathList,$targetFilePath))
{
break;
}
$returnInfo = true;
} while (0);
return $returnInfo;
}
/**
* 本为目录名排序.
*/
// public function taxisFolder($a,$b)
// {
// $a = strlen($a);
// $b = strlen($b);
// if ($a == $b) {
// return 0;
// }
// return ($a > $b) ? -1 : 1;
//
// }

}
$direr = new Direr();
$sourceFolder = 'Styles/'; # 源文件夹
$targetFolder = 'SSTTYYLLEESS/'; #目标文件夹
//$direr->setupFolder($sourceFolder,'*');
//print_r($direr->getFilePath());
echo $direr->copyFolder($sourceFolder,$targetFolder,'*');
#操作 最后一个参数如果是*即是复制所有文件. 目标文件根目录必须已存在(这是为了防止,错误的目录建立)
?>








































































































































































































































