| 
       本文章给大家分享的文件操作类包括对文件和文件夹创建,复制,移动和删除,有需要对文件操作学习的同学可进入参考参考。 
实例代码如下: 
	
	- <?php 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- class FileUtil { 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function createDir($aimUrl) { 
 
	-        $aimUrl = str_replace('', '/', $aimUrl); 
 
	-        $aimDir = ''; 
 
	-        $arr = explode('/', $aimUrl); 
 
	-        foreach ($arr as $str) { 
 
	-          $aimDir .= $str . '/'; 
 
	-          if (!file_exists($aimDir)) { 
 
	-             mkdir($aimDir); 
 
	-          } 
 
	-        } 
 
	- } 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function createFile($aimUrl, $overWrite = false) { 
 
	-        if (file_exists($aimUrl) && $overWrite == false) { 
 
	-          return false; 
 
	-        } elseif (file_exists($aimUrl) && $overWrite == true) { 
 
	-          FileUtil::unlinkFile($aimUrl); 
 
	-        } 
 
	-        $aimDir = dirname($aimUrl); 
 
	-        FileUtil::createDir($aimDir); 
 
	-        touch($aimUrl); 
 
	-        return true; 
 
	- } 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function moveDir($oldDir, $aimDir, $overWrite = false) { 
 
	-        $aimDir = str_replace('', '/', $aimDir); 
 
	-        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/'; 
 
	-        $oldDir = str_replace('', '/', $oldDir); 
 
	-        $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/'; 
 
	-        if (!is_dir($oldDir)) { 
 
	-          return false; 
 
	-        } 
 
	-        if (!file_exists($aimDir)) { 
 
	-          FileUtil::createDir($aimDir); 
 
	-        } 
 
	-        @$dirHandle = opendir($oldDir); 
 
	-        if (!$dirHandle) { 
 
	-          return false; 
 
	-        } 
 
	-        while(false !== ($file = readdir($dirHandle))) { 
 
	-          if ($file == '.' || $file == '..') { 
 
	-             continue; 
 
	-          } 
 
	-          if (!is_dir($oldDir.$file)) { 
 
	-             FileUtil::moveFile($oldDir . $file, $aimDir . $file, $overWrite); 
 
	-          } else { 
 
	-             FileUtil::moveDir($oldDir . $file, $aimDir . $file, $overWrite); 
 
	-          } 
 
	-        } 
 
	-        closedir($dirHandle); 
 
	-        return rmdir($oldDir); 
 
	- } 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function moveFile($fileUrl, $aimUrl, $overWrite = false) { 
 
	-        if (!file_exists($fileUrl)) { 
 
	-          return false; 
 
	-        } 
 
	-        if (file_exists($aimUrl) && $overWrite = false) { 
 
	-          return false; 
 
	-        } elseif (file_exists($aimUrl) && $overWrite = true) { 
 
	-          FileUtil::unlinkFile($aimUrl); 
 
	-        } 
 
	-        $aimDir = dirname($aimUrl); 
 
	-        FileUtil::createDir($aimDir); 
 
	-        rename($fileUrl, $aimUrl); 
 
	-        return true; 
 
	- } 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function unlinkDir($aimDir) { 
 
	-        $aimDir = str_replace('', '/', $aimDir); 
 
	-        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/'; 
 
	-        if (!is_dir($aimDir)) { 
 
	-          return false; 
 
	-        } 
 
	-        $dirHandle = opendir($aimDir); 
 
	-        while(false !== ($file = readdir($dirHandle))) { 
 
	-          if ($file == '.' || $file == '..') { 
 
	-             continue; 
 
	-          } 
 
	-          if (!is_dir($aimDir.$file)) { 
 
	-             FileUtil::unlinkFile($aimDir . $file); 
 
	-          } else { 
 
	-             FileUtil::unlinkDir($aimDir . $file); 
 
	-          } 
 
	-        } 
 
	-        closedir($dirHandle); 
 
	-        return rmdir($aimDir); 
 
	- } 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function unlinkFile($aimUrl) { 
 
	-        if (file_exists($aimUrl)) { 
 
	-          unlink($aimUrl); 
 
	-          return true; 
 
	-        } else { 
 
	-          return false; 
 
	-        } 
 
	- } 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function copyDir($oldDir, $aimDir, $overWrite = false) { 
 
	-        $aimDir = str_replace('', '/', $aimDir); 
 
	-        $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/'; 
 
	-        $oldDir = str_replace('', '/', $oldDir); 
 
	-        $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir.'/'; 
 
	-        if (!is_dir($oldDir)) { 
 
	-          return false; 
 
	-        } 
 
	-        if (!file_exists($aimDir)) { 
 
	-          FileUtil::createDir($aimDir); 
 
	-        } 
 
	-        $dirHandle = opendir($oldDir); 
 
	-        while(false !== ($file = readdir($dirHandle))) { 
 
	-          if ($file == '.' || $file == '..') { 
 
	-             continue; 
 
	-          } 
 
	-          if (!is_dir($oldDir . $file)) { 
 
	-             FileUtil::copyFile($oldDir . $file, $aimDir . $file, $overWrite); 
 
	-          } else { 
 
	-             FileUtil::copyDir($oldDir . $file, $aimDir . $file, $overWrite); 
 
	-          } 
 
	-        } 
 
	-        return closedir($dirHandle); 
 
	- } 
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function copyFile($fileUrl, $aimUrl, $overWrite = false) { 
 
	-        if (!file_exists($fileUrl)) { 
 
	-          return false; 
 
	-        } 
 
	-        if (file_exists($aimUrl) && $overWrite == false) { 
 
	-          return false; 
 
	-        } elseif (file_exists($aimUrl) && $overWrite == true) { 
 
	-          FileUtil::unlinkFile($aimUrl); 
 
	-        } 
 
	-        $aimDir = dirname($aimUrl); 
 
	-        FileUtil::createDir($aimDir); 
 
	-        copy($fileUrl, $aimUrl); 
 
	-        return true; 
 
	- } 
 
	- } 
 
	- ?> 
 
	 
 
      
      (责任编辑:admin) |