现在的位置: 首页 > 综合 > 正文

dirmanage.php

2012年02月13日 ⁄ 综合 ⁄ 共 5898字 ⁄ 字号 评论关闭
<?
  
/****************************************
  名称:  dirmanage.php
    作者:  psdshow
                 联系方式: psdshow@yahoo.com.cn
    版本号:  1.0
  版权:
    编写日期: 2005年12月11日
    文件描述: 用于文件夹的操作(建立,删除,移动)
    修改日志:
  ****************************************
*/
class DirManage{
var $dir="";
      
//构造函数
      function DirManage(){
                    ;
                    }

        //EchoStr:仅作测试输出用途
        function EchoStr($str){
          
echo "<pre>";
          
print_r($str);
          
echo "</pre>";
          
exit;
         }
 
//处理路径,将 \,\\转换为/,如果路径末尾不包含/则加个/
         function DealDir($dir){
  
if(empty($dir)){
                         
$this->ErrorIs("路径不能为空");
                         }
                 
if(strpos($dir,"\\")){
                         
$dir=str_replace("\\","/",$dir);
                         }
                 
if(substr($dir,-1)!="/"){
                         
$dir.="/";
                         }
                 
return $dir;
          }
         
/*
         将一个文件的所有文件读入数组 ,
         
*/
         
function _readdir($dir){
                 
if(is_dir($dir)){
                         
$dir_handle=@opendir($dir) or $this->ErrorIs("{$dir}目录不可读取");
                         
while(false!==($file_handle=readdir($dir_handle))){
                                 
if($file_handle=="." or $file_handle=="..")continue;
                                 
if(is_dir($dir.$file_handle))$file[]=$dir.$file_handle."/";
                                 
else $file[]=$dir.$file_handle;
                                 }
                         }
                 
return $file;
                 }
         
//建立文件夹
         function _mkDir($dir,$mode=0755){
                 
$dir=$this->DealDir($dir);
                 
if(strpos($dir,"/")){
                     
$dir_info=explode("/",$dir);
                     
foreach($dir_info as $key=>$value){
                         
$dir_path.=$value."/";
                         
if(!file_exists($dir_path)){
                             @
mkdir($dir_path,$mode) or $this->ErrorIs("建立文件夹{$dir_path}时失败了");
                             }
else{
                             
continue;
                             }
                         }
                     }
else{
                     @
mkdir($dir,$mode) or $this->ErrorIs("{$dir}建立失败了,请检查权限");
                     }
                     
return true;
                 }
         
//删除文件夹,在删除前调用$this->DealDir和检测是否存在目录
         function _rmDir($dir){
                 
if(is_dir($dir)){
                         
$dir_handle=@opendir($dir) or $this->ErrorIs("目录{$dir}不可读取");
                         
while(false!==($file_handle=readdir($dir_handle))){
                                 
if($file_handle=="." or $file_handle=="..")continue;
                                 
$this->_rmDir($dir."/".$file_handle);
                                 }
                         
closedir($dir_handle);
                         
if(!rmdir($dir)){
                                 
$this->ErrorIs("文件夹{$dir}删除失败");
                                 }
                         }
else{
                         
unlink($dir);
                         }
                 
$this->_clear();
                 
return true;
                 }
         
//移动单个文件
         function _movefile($src,$dst,$overwrite=1,$keep=0){
                 
if(!file_exists($src)){
                         
$this->ErrorIs("{$src}源文件不存在");
                         }
                 
if(empty($dst)){
                         
$this->ErrorIs("没有指定目标文件{$dst}");
                         }
                 
if(file_exists($dst)){
                         
if($overwrite==1){
                                 
unlink($dst);
                                 }
else{
                                 
return false;
                                 }
                         }
                 
if(copy($src,$dst)){
                         
if($keep==0){
                                 
unlink($src);
                                 }
                         
return true;
                         }
                 }
         
/*
          移动文件夹,可以是文件->文件夹,也可以是文件到文件夹
         $overwrite => 1 如果目标存在则覆盖
         $keep => 1 保留源文件
         set_time_limit(1000)执行大的文件移动时更改页面执行时间
         在执行此函数前,执行$this->DealDir();修正源和目标路径
         开始的判断总觉得不该在这里面判断~.~
         
*/
         
function _moveDir($src,$dst,$overwrite=1,$keep=1){
                 
if(!file_exists($src)){
                         
$this->ErrorIs("{$src}源文件不存在");
                         }
                 
if(is_dir($dst) and $overwrite==1){
                         
$this->_rmDir($dst);
                         }
                 
if(!is_dir($dst)){
                         
$this->_mkdir($dst,0755);
                         }
                 
if(is_dir($src)){
                         
$src_handle=@opendir($src) or $this->ErrorIs("目录{$src}无读取权限");
                         
while(false!==($file_handle=readdir($src_handle))){
                                 
if($file_handle=="." or $file_handle=="..")continue;
                                 
if(is_dir($src.$file_handle)){
                                         
$this->_moveDir($src.$file_handle."/",$dst.$file_handle."/",$overwrite,$keep);
                                         }
else{
                                         
$this->_movefile($src.$file_handle,$dst.$file_handle,$overwrite,$keep);
                                         }
                                 }
                         
closedir($src_handle);
                         
if($keep!=1){
                                 
$this->_rmDir($src);
                                 }
                         
$this->_clear();
                         }
                 
return (file_exists($dst));
                 }

         //清除缓存
         function _clear(){
                 
clearstatcache();
                 }

         function ErrorIs($msg){
                 
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">";
                 
echo "$msg";
                 
exit;
                 }

}
?>

抱歉!评论已关闭.