<?php /** * Created by PhpStorm. * User: ver * Date: 16-3-4 * Time: 上午10:46 */ class RmoveDir{ public $error = true; public function __construct(){ } /** * 删除文件夹, 同时删除文件夹下的所有文件 */ public function rm_dir($path){ // 检查文件或目录是否存在 if(!file_exists($path)) { $this->set_error( $path.' 不存在' ); return false; } // 判断是否是目录:目录,递归删除;文件,直接删除 if ( is_dir($path) ){ $handle = opendir($path); while (($file = readdir($handle)) !== false){ // 目录解释:.子目录,..父级目录 if( $file != '.' && $file != '..' ){ // 递归调用删除所有文件 $this->rm_dir( $path.DS.$file ); } } closedir($handle); rmdir($path); }else{ unlink($path); } return true; } /** * 建立文件夹, 支持多级文件夹 如 aaa\bbb\ccc\... */ public function mk_dir($path, $mode = 0777) { $dirs = explode(DS,$path); $dir = ''; // 逐级判断目录是否存在,不存在则是需要创建的文件夹 for ($i=0, $n=count($dirs); $i<$n; $i++) { $dir .= $dirs[$i].DS; if (!file_exists($dir)) mkdir($dir,$mode); } } /** * 复制文件夹 */ public function copy_dir($src, $dst, $overwrite = false) { if(!is_dir($dst)) $this->mk_dir($dst); $handle = opendir($src); if($handle) { while(false !== ($file = readdir($handle))) { if($file != '.' && $file != '..') { $path = $src.DS.$file; if(is_file($path)) { if(!is_file($dst.DS.$file) || $overwrite) @copy($path, $dst.DS.$file); } else { if(!is_dir($dst.DS. $file)) mkdir($dst.DS.$file); $this->copy_dir($path, $dst.DS.$file, $overwrite); } } } } closedir($handle); } public function set_error( $error ){ $this->error = $error; } public function get_error(){ return $this->error; } }
PHP文件夹管理类
最新推荐文章于 2021-03-24 12:53:03 发布