为了账号安全,请及时绑定邮箱和手机立即绑定

我自己写的文件目录操作的类,谢谢指教

标签:
PHP
<?php
/**
 *开源项目:所有关于文件目录操作的功能
 *操作1:文件上传功能
 *操作2:文件下载
 *操作3:查看单级目录结构
 *操作4:递归查看目录结构
 *操作5:查看文件或文件夹的大小
 *操作6:按关键字递归查找文件
 *操作7:递归删除非空目录
 *操作8:递归删除空目录
 *操作9:递归创建目录
 *操作10:剪切目录或文件
 */

class File{
    /**
     *文件上传
     *@param $file array 要上传的文件数组信息
     *@param $allow array 允许上传的文件类型
     *@param $size int 允许上传文件的大小
     *@param $path string 上传文件保存的路径
     *@return $fileName string 文件上传成功后的新文件名
     */
    public function upload($file,$allow,$size,$path='./upload'){
        //判断上传文件是否是一个合理的文件
        if(!is_array($file)){
            echo '上传文件不是一个合理的文件';
            return false;
        }
        //判断上传文件是否是允许上传文件的类型
        if(!in_array($file['type'],$allow)){
            echo '不允许上传的文件类型';
            return false;
        }
        //判断上传文件的大小是否符合允许上传文件的大小
        if($file['size']>$size){
            echo '上传文件大小超过允许上传文件大小';
            return false;
        }
        //判断文件是否是通过HTTP POST上传的
        if(!is_uploaded_file($file['tmp_name'])){
            echo '文件不是通过http上传的';
            return false;
        }
        //文件上传错误判断
        if($file['error']>0){
            switch($file['error']){
                case 1:
                    echo '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
                    return false;
                break;
                case 2:
                    echo '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
                    return false;
                break;
                case 3:
                    echo '文件只有部分被上传';
                    return false;
                break;
                case 4:
                    echo '没有文件被上传';
                    return false;
                break;
                case 6:
                    echo '找不到临时文件夹';
                    return false;
                break;
                case 7:
                    echo '文件写入失败';
                    return false;
                break;
            }
        }
        //判断上传文件保存路径是否存在
        if(!file_exists($path)){
            mkdir($path);
        }
        //生成新的文件名
        $newName = $this->getNewFileName($file['name']);
        if(move_uploaded_file($file['tmp_name'],$path.'/'.$newName)){
            return $newName;
        }else{
            echo '文件上传失败';
            return false;
        }
    }

    /**
     *文件下载
     *@param $filePath string 要下载的文件的路径
     */
    public function downLoad($filePath){
        $basename = basename($filePath);
        header("Content-Type:application/octet-stream");
        header("Content-Disposition:attachment;filename=".$basename);
        echo file_get_contents($filePath);
    }

    /**
     *生成新的文件名
     *@param $oldName string 旧的文件名
     *@return $newName string 新的文件名
     */
    public function getNewFileName($oldName){
        $newName = '';
        //获取文件的后缀名
        $ext = pathinfo($oldName)['extension'];
        //生成六位随机数
        $chars = array_merge(range('a','z'),range('A','Z'));
        //打乱数组的顺序
        shuffle($chars);
        $newName .= implode(array_slice($chars,0,6),'');
        $newName .= time();
        $newName .= '.'.$ext;
        return $newName;
    }

    /**
     *查看单级目录结构
     *@param $path string 要查看的目录路径
     *@param $arr array 目录结构
     */
    public function showDiretory($path){
        $arr = array();
        //判断路径是否存在
        if(!file_exists($path)){
            echo '文件或目录不存在';
            return false;
        }
        if(is_dir($path)){
            $handle = opendir($path);
            while(false !== $file = readdir($handle)){
                $subPath = $path . '/' . $file;
                if(is_dir($subPath)){
                    $arr['dir'][] = $file;
                }else{
                    $arr['file'][] = $file;
                }
            }
            closedir($handle);
        }else{
            $arr['file'][] = basename($path);
        }
        return $arr;
    }

    /**
     *递归查看目录结构
     *@param $path string 目录路径
     *@return $arr array 目录结构
     */
    public function showDiretorys($path){
        static $arr = array();
        //判断路径是否存在
        if(!file_exists($path)){
            echo '文件或目录不存在';
            return false;
        }
        if(is_dir($path)){
            $handle = opendir($path);
            $flag = true;
            while(false !== $file = readdir($handle)){
                $subPath = $path . '/' . $file;
                $subPath = str_replace('//','/',$subPath);
                if($file=='.'||$file=='..'){
                    continue;
                }
                if(is_dir($subPath)){
                    $this->showDiretorys($subPath);
                }else{
                    $flag = false;
                    $arr[dirname($subPath)][] = $file;
                }
                if($flag){
                    $arr[$path] = null;
                }
            }
            closedir($handle);
        }else{
            $arr[] = $path;
        }
        return $arr;
    }

    /**
     *查看文件或文件夹的大小
     *@param $path string 要查看大小的文件或目录路径
     *@return $size float 文件或目录的大小
     */
    public function dirSize($path){
        static $size = 0;
        //判断文件或目录是否存在
        if(!file_exists($path)){
            echo '文件或目录不存在';
            return false;
        }
        if(is_dir($path)){
            $handle = opendir($path);
            while(false !== $file = readdir($handle)){
                $subPath = $path . '/' . $file;
                if($file=='.'||$file=='..'){
                    continue;
                }
                if(is_dir($subPath)){
                    $this->getSize($subPath);
                }else{
                    $size += filesize($subPath);
                }
            }
            closedir($handle);
        }else{
            return filesize($path);
        }
        return $this->getSize($size);
    }

    /**
     *返回带有单位的文件大小
     *@param $size int 原始的文件大小
     *@return $hsize float 带有单位的文件大小
     */
    public function getSize($size){
        $arr = array('Byte','KB','MB','GB','TB');
        $i = 0;
        while($size>1024){
            $size /= 1024;
            $i++;
        }
        $hsize = round($size,2);
        $hsize .= $arr[$i];
        return $hsize;
    }

    /**
     *按关键字递归查找文件
     *@param $path string 要查找文件的目录
     *@param $keyword string 要查找的关键字
     *@return $files array 查找到的文件数组
     */
    public function findFilesByKeyword($path,$keyword){
        static $files = array();
        //判断文件或目录是否存在
        if(!file_exists($path)){
            echo '文件或目录不存在';
            return false;
        }
        if(is_dir($path)){
            $handle = opendir($path);
            while(false !== $file = readdir($handle)){
                $subPath = $path . '/' . $file;
                $subPath = str_replace('//','/',$subPath);
                if($file=='.'||$file=='..'){
                    continue;
                }
                if(is_dir($subPath)){
                    $this->findFilesByKeyword($subPath,$keyword);
                }else{
                    if(strpos($file,$keyword)!==false){
                        $files[] = $subPath;
                    }
                }
            }
            closedir($handle);
        }
        return $files;
    }

    /**
     *递归删除非空目录
     *@param $path string 要删除的目录
     */
    public function deleteDir($path){
        //判断目录是否存在
        if(!file_exists($path)){
            echo '文件或目录不存在';
            return false;
        }
        if(is_dir($path)){
            $handle = opendir($path);
            while(false !== $file = readdir($handle)){
                if($file=='.'||$file=='..'){
                    continue;
                }
                $subPath = $path . '/' . $file;
                if(is_dir($subPath)){
                    $this->deleteDir($subPath);
                }else{
                    unlink($subPath);
                }
            }
            closedir($handle);
            if(rmdir($path)){
                return true;
            }else{
                echo '删除失败';
                return false;
            }
        }else{
            unlink($path);
        }
    }

    /**
     *递归删除空目录
     *@param $path string 要删除的目录路径
     */
    public function deleteEmptyDir($path){
        //判断目录路径是否存在
        if(!file_exists($path)){
            echo '文件或目录不存在';
            return false;
        }
        if(is_dir($path)){
            $handle = opendir($path);
            $flag = true;
            while(false !== $file = readdir($handle)){
                if($file=='.'||$file=='..'){
                    continue;
                }
                $subPath = $path . '/' . $file;
                if(is_dir($subPath)){
                    $this->deleteEmptyDir($subPath);
                }
                $flag = false;
            }
            closedir($handle);
            if($flag){
                if(rmdir($path)){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    /**
     *递归创建目录
     *@param $path string 在此目录下创建目录
     *@param $destination string 创建的目标目录
     */
    public function makeDir($path,$destination){
        $destination = str_replace('\\','/',$destination);
        //判读目录是否存在
        if(!file_exists($path)){
            echo '目录不存在';
            return false;
        }
        if(is_dir($path)){
            $handle = opendir($path);
            $dirs = explode('/',$destination);
            $subPath = $path . '/' . $dirs[0];
            if(!file_exists($subPath)){
                if(!mkdir($subPath)){
                    echo '目录创建失败';
                    return false;
                }
            }
            if(count($dirs)>1){
                $dir = implode('/',array_slice($dirs,1));
                $this->makeDir($subPath,$dir);
            }
            closedir($handle);
            return true;
        }
    }

    /**
     *剪切目录或文件
     *@param $source string 源文件或目录路径
     *@param $destination string 目标文件或目录路径
     */
    public function moveDirToDestination($source,$destination){
        //判断目录或者文件是否存在
        if(!file_exists($source)||!file_exists($destination)){
            echo '文件或目录不存在';
            return false;
        }
        if(strpos($destination,$source)===0){
            echo '目标目录是源目录的子目录';
            return false;
        }
        $sourceDirs = $this->showDiretorys($source);
        $sourceDir = array_keys($sourceDirs);
        $sourcePrev = substr($source,0,strrpos($source,'/')+1);
        $subSourceDirs = array();
        foreach($sourceDir as $key=>$val){
            $subSourceDirs[$key] = ltrim($val,$sourcePrev);
        }
        //在目标目录中创建源目录中相应的目录
        foreach($subSourceDirs as $val){
            $this->makeDir($destination,$val);
        }
        //复制文件
        foreach($sourceDirs as $key=>$val){
            if($val){
                foreach($val as $v){
                    //源文件路径
                    $fileSourcePath = $key.'/'.$v;
                    //目标文件路径
                    $fileDestinationPath = $destination.'/'.ltrim($fileSourcePath,$sourcePrev);
                    copy($fileSourcePath,$fileDestinationPath);
                }
            }
        }
        //删除原目录
        $this->deleteDir($source);
        return true;
    }
}
header('content-type:text/html;charset=utf-8');
$fileObj = new File();
$dirs = $fileObj->moveDirToDestination('./777','./000');
//$dirs = $fileObj->deleteDir('./huhu');
echo '<pre>';
var_dump($dirs);
?>
点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
PHP开发工程师
手记
粉丝
29
获赞与收藏
33

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消