PHP文件夹管理类

<?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;
    }



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值