PHP文件操作类

不羁的心 提交于 2020-03-01 17:39:49
<?php
     //文件管理类
     
     class  myfile  {
          
         
         public function __construct() {
             return true;
         }
         
         //建立文件夹
         public function mkDir($path) {
            $path = str_replace("\\","/",$path);
            
            $aimDir = "";
            
            //分隔文件路径
            $array = explode("/",$path);
            
            foreach($array as $v) {
                if($v != "") {
                   $aimDir .= $v."/";
                   
                   if(!$this->isExists($aimDir)) {
                          mkdir($aimDir);
                   }
                } 
            
            }
         }
         
         
         //建立某个文件夹下面的某个文件,后面一个参数代表是否覆盖之前的文件
         public function touchFile($path, $overWrite = false) {
             //如果文件存在,并不覆盖之前的文件
             if($this->isExists($path) && $overWrite == false) {
                     return false;
             }
             
             
             //文件存在,并覆盖操作
             $dir = dirname($path);
             
                     
             //判断目录是否存在,不存在创建目录
             if(!$this->isExists($dir)) {
                 $this->mkDir($dir);
             }
                     
             //创建文件
             touch($path);
                     
             return true;
                       
         }
         
         //移动文件或文件夹
         public function mv($filePath, $aimPath, $overWrite = false) {
            
             if($this->isDir($filePath)){
                 $this->mvDir($filePath,$aimPath,$overWrite);
             }else{
                 $this->mvFile($filePath,$aimPath,$overWrite);
             }            
            
         }
         
         //复制文件或文件夹
         public function cp($filePath, $aimPath, $overWrite = false) {
             
             if($this->isDir($filePath)) {
                 $this->cpDir($filePath, $aimPath, $overWrite);
             }else{
                 $this->cpFile($filePath, $aimPath, $overWrite);
             }
             
         }
         
         //删除文件或目录
         public function rm($filePath) {
             
             if($this->isDir($filePath)) {
                 $this->rmDir($filePath);
             }else{
                 $this->rmFile($filePath);
             }
         }         
         
         
         //统计文件或目录大小
         public function size($filePath) {
             
             if($this->isDir($filePath)) {
                 return $this->sizeCount($this->dirSize($filePath));
             }else{
                 return $this->sizeCount($this->fSzie($filePath));
             }
         }
         
         //统计目录大小
         public function dirSize($dirPath) {
             //是否存在,并且是否是目录
             if(!$this->isExists($dirPath) || !is_dir($dirPath)) {
                   return false;
             } 
             
             $handle = @opendir($dirPath);
             
             if(!$handle) {
                 return false;
             }
             
             $dirSize = 0;
             
             //遍历目录
             while($file = @readdir($handle)) {
                 if($file == "." || $file == "..") {
                     continue;
                 }
                 
                 $path = $dirPath.DIRECTORY_SEPARATOR.$file;
                 
                 if($this->isDir($path)){
                     $dirSize += $this->dirSize($path);
                 }else{
                     $dirSize += $this->fSize($path);
                 }
             }
             
             closedir($handle);
             
             return $dirSize;
         }
         
         
         
         //统计文件大小
         public function fSize($fileName) {
             //判断是否存在
             if(!$this->isExists($fileName) || !is_file($fileName)) {
                  return false;
             }
             
             
             return filesize($fileName);
         }
         
         
         
         //文件大小单位换算
         public function sizeCount($fileSize) {
             $suffix = "";
             
             if($fileSize > pow(2,40)) {
                  $size = round($fileSize/pow(2,40),2);
                  
                  $suffix = "TB";
             }elseif($fileSize > pow(2,30)) {
                  $size = round($fileSize/pow(2,30),2);
                  
                  $suffix = "GB";
             }elseif($fileSize > pow(2,20)) {
                  $size = round($fileSize/pow(2,20),2);
                  
                  $suffix = "MB";
             }elseif($fileSize > pow(2,10)) {
                  $size = round($fileSize/pow(2,10),2);
                  
                  $suffix = "KB";
             }else{
                  $size = $fileSize;
                  
                  $suffix = "bytes";
             }
             
             
             return $size.$suffix;
             
             
         }
         
         
         //写数据
         public function write($fileName,$content, $append = false) {
             //判断目录是否存在
             if(!$this->isExists(dirname($fileName))) {
                 $this->mkDir(dirname($fileName));
             }
             
             //是否是追加的模式
             $type = $append ? "ab" : "wb";
             
             if(!$fp = @fopen($fileName, $type)) {
                  return false;
             }
             
             @flock($fp,LOCK_EX);
             
             $OK = fwrite($fp, $content);
             
             @flock($fp,LOCK_UN);
             
             fclose($fp);
             
             return $ok;
             
         
         }
         
         
         
         
         
         //读取一行数据
         public function readLine($fileName, $size = 4096) {
             static $fileArry = array();
             
             if(!$fileArry[$fileName]) {
                 $fileArry[$fileName] = fopen($fileName, "rb");  
             }
             
             
             $fp = $fileArry[$fileName];
             
             if($fp && !feof($fp)) {
                 return fgets($fp, $size);
             }
             
             fclose($fp);
             
             unset($fileArry[$fileName]);
             
             return false;
             
             
         }
         
         
         
         //读取某个文件的所有数据
         public function readAll($fileName) {
             if(!is_file($fileName)) {
                 return false;
             }
             
             return file_get_contents($fileName);
         }
         
         
         //复制目录
         public function cpDir($dirPath, $aimPath, $overWrite) {
             //路径符号
             $dirPath = str_replace("\\","/",$dirPath);
             
             $dirPath = rtrim($dirPath,'/').'/';
             
             $aimPath = str_replace("\\","/",$aimPath);
             
             $aimPath = rtrim($aimPath,'/').'/';
            
         
             //判断目录是否存在
             if(!$this->isExists($dirPath)) {
                return false;
             }
             
             if(!$this->isExists($aimPath)) {
                 $this->mkDir($aimPath);
             }
             
             //打开目录
             $handle = @opendir($dirPath);
             
             if(!$handle) {
                 return false;
             }
             
             //遍历目录
             $handle = @opendir($dirPath);
             
             if(!$handle) {
                 return false;
             }
             
             while($file = @readdir($handle)) {
                 if($file == "." || $file == "..") {
                     continue;
                 }
                 
                 //判断是文件还是目录
                 if($this->isDir($dirPath.$file)) {
                     $this->cpdir($dirPath.$file,$aimPath.$file,$overWrite);
                 }else{
                     $this->cpFile($dirPath.$file,$aimPath.$file,$overWrite);
                 }
             }
             
             closedir($handle);
             
             return true;
             
             
         }
         
         
         
         //复制文件
         public function cpFile($filePath, $aimPath, $overWrite) {
             //判断文件是否存在
             if(!$this->isExists($filePath)) {
                 return false;
             } 
             
             if($this->isExists($aimPath)  && $overWrite == false) {
                  return false;
             }elseif($this->isExists($aimPath) && $overWrite == true) {
                  $this->rmFile($aimPath);
             }
             
             //获取要移动目录的目录名称
             $dir = dirname($aimPath);
             
             //判断目录是否存在
             if(!$this->isExists($dir)) {
                 $this->mkDir($dir);
             }
             
             //复制文件
             copy($filePath,$aimPath);
             
             return true;
         }
         
         
         
         //移动文件夹
         public function mvDir($filePath, $aimPath, $overWrite = false) {
             //去掉多余的/
             $filePath = str_replace("\\",'/',$filePath);
             
             //补齐右边的/
             $filePath = rtrim($filePath,"/").'/';
             
             $aimPath = str_replace("\\","/",$aimPath);
             
             $aimPath = rtrim($aimPath,"/")."/";
             
             //判断是否是目录
             if(!$this->isDir($filePath)) {
                 return false;
             }
             
             //判断目录路径是否存在
             if(!$this->isExists($aimPath)) {
                 $this->mkDir($aimPath);
             }
             
             //打开文件夹
             $handle = @opendir($filePath);

             if(!$handle) {
                 return false;
             }              
             
             //遍历文件夹
             while($file = readdir($handle) ){
                 if($file == "." || $file == "..") {
                     continue;
                 }
                 
                 if($this->isDir($filePath.$file)) {
                     $this->mvDir($filePath.$file,$aimPath.$file,$overWrite);
                 }else{
                     $this->mvFile($filePath.$file, $aimPath.$file, $overWrite);
                 }
             }
             
             closedir($handle);
             
             return true;
         }
         
         //移动文件
         public function mvFile($filePath, $aimPath, $overWrite) {
              //判断移动文件是否存在
              if(!$this->isExists($filePath)) {
                  return false;
              }
              
              //如果目标文件存在,并且不覆盖
              if($this->isExists($aimPath) && $overWrite == false) {
                  return false;
              }elseif($this->isExists($aimPath && $overWrite == true)){
                  //覆盖,删除原有文件
                  $this->rmFile($aimPath);
              }
              
              //获取要移动文件的目录部分
              $dir = dirname($aimPath);
              
              //判断是否存在该目录,不存在则创建该目录
              if(!$this->isExists($dir)) {
                 $this->mkDir($dir);
              }
              
              //移动文件
              rename($filePath,$aimPath);
              
              return true;
         }
         
         
         //删除文件夹
         public function rmDir($dirPath) {
             //去掉多余的路径符号
             $dirPath = str_replace("\\","/",$dirPath);
             
             //去掉右边多余的/
             $dirPath = rtrim($dirPath,"/").'/';
             
             //判断是否是目录
             if(!$this->isDir($dirPath)) {
                 return false;
             }
             
             //打开文件夹
             $handle = @opendir($dirPath);
             
             while($file = readdir($handle)) {
                 if($file == "." || $file == "..") {
                      continue;
                 }
                 
                 //判断是目录还是文件
                 if(!$this->isDir($dirPath.$file)) {
                     $this->rmFile($dirPath.$file);
                 }else{
                     $this->rmDir($dirPath.$file);
                 }
             }
             
             closedir($handle);
             
             //最后删除空文件夹
             return rmdir($dirPath);
             
         }
         
         
         //删除文件
         public function rmFile($fileName) {
             //判断文件是否存在
             if($this->isExists($fileName)) {
                 @unlink($fileName);
                
                 return true;
             }else{
                return false;
             }
         } 
         
         
         //判断是否是一个文件夹
         public function isDir($path) {
            return @is_dir($path);
         }
         
         //判断文件是否存在
         public function isExists($path) {
            return @file_exists($path);
         }
       
     }
     

?>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!