复制文件目录的类

<?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,'*');
#操作 最后一个参数如果是*即是复制所有文件. 目标文件根目录必须已存在(这是为了防止,错误的目录建立)
?>  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值