kavo

php 快速读取文件夹下文件列表

因为学习某个项目,需要文件函数,为了防止生疏,特意记载下来。

1:读取指定目录下的所有文件,或者匹配指定后缀的文件列表

//scadir函数负责扫描指定文件夹下的内容
$list = scandir('./');
//获取目录下所有php结尾的文件列表
$list = glob('*.php');

2:使用循环方式获取,注:$f就是一个文件目录,这里只是采用tp框架获取目录的写法

        $f = Env::get('APP_PATH');
        $resource = opendir($f.'index\lang');
        while ($rows = readdir($resource)){
            if($rows == "." || $rows == "..") {continue;}
            var_dump($rows);
        }

3:递归获取所有文件名

    public function getFiles($path,&$finename)
    {
        //这里也可以使用dir方法
        //$resource = dir($path);
        //while里条件写成$rows = $resource->read();
        $resource = opendir($path);
        while ($rows = readdir($resource)){
            if( is_dir($path.'/'.$rows) && $rows != "." && $rows != "..")
            {
                $this->getFiles($path.'/'.$rows,$finename);
            }elseif($rows != "." && $rows != "..")
            {
                $finename[] = $rows;
            }
        }
    }
    public function index()
    {
        $f = Env::get('APP_PATH');
        $path = $f.'index';
        $finename = [];
        $lists = $this->getFiles($path,$finename);
        var_dump($finename);
        die;
        return $this->fetch();
    }

附带一个完整的函数,方便使用各种查询

/*
 * 获取指定目录下指定文件后缀的函数
 * @$path   文件路径
 * @$ext    文件后缀名,默认为false为不指定,如果指定,请以数组方式传入
 * @$filename   使用时请提前赋值为空数组
 * @$recursive  是否递归查找,默认为false
 * @$baseurl    是否包含路径,默认包含
 */
    function getDirFilesLists($path,&$filename,$recursive = false,$ext = false,$baseurl = true){

        if(!$path){
            die('请传入目录路径');
        }
        $resource = opendir($path);
        if(!$resource){
            die('你传入的目录不正确');
        }
        //遍历目录
        while ($rows = readdir($resource)){
            //如果指定为递归查询
            if($recursive) {
                if (is_dir($path . '/' . $rows) && $rows != "." && $rows != "..") {
                    getDirFilesLists($path . '/' . $rows, $filename,$resource,$ext,$baseurl);
                } elseif ($rows != "." && $rows != "..") {
                    //如果指定后缀名
                    if($ext) {
                        //必须为数组
                        if (!is_array($ext)) {
                            die('后缀名请以数组方式传入');
                        }
                        //转换小写
                        foreach($ext as &$v){
                            $v = strtolower($v);
                        }
                        //匹配后缀
                        $file_ext = strtolower(pathinfo($rows)['extension']);
                        if(in_array($file_ext,$ext)){
                            //是否包含路径
                            if($baseurl) {
                                $filename[] = $path . '/' . $rows;
                            }else{
                                $filename[] = $rows;
                            }
                        }
                    }else{
                        if($baseurl) {
                            $filename[] = $path . '/' . $rows;
                        }else{
                            $filename[] = $rows;
                        }
                    }
                }
            }else{
                //非递归查询
                if (is_file($path . '/' . $rows) && $rows != "." && $rows != "..") {
                    if($baseurl) {
                        $filename[] = $path . '/' . $rows;
                    }else{
                        $filename[] = $rows;
                    }
                }
            }
        }
    }


posted on   下雨天唱情歌  阅读(79)  评论(0编辑  收藏  举报  

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示