PHP文件夹管理类
2016-03-04 11:56
746 查看
<?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; } }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表
- C语言实现的统计php代码行数功能源码(支持文件夹、多目录)
- php xml 入门学习资料