php遍历文件夹和文件列表示例分享


Posted in PHP onMarch 11, 2014

为PHP遍历目录和文件列表写了一个简单的类,并附上使用实例,大家参考使用吧

<?php
define('DS', DIRECTORY_SEPARATOR);
class getDirFile{
    //返回数组
    private $DirArray  = array();
    private $FileArray = array();
    private $DirFileArray = array();
    private $Handle,$Dir,$File;
    //获取目录列表
    public function getDir( & $Dir ){
        if( is_dir($Dir) ){
            if( false != ($Handle = opendir($Dir)) ){
                while( false != ($File = readdir($Handle)) ){
                    if( $File!='.' && $File!='..' && !strpos($File,'.') ){
                        $DirArray[] = $File;
                    }
                }
                closedir( $Handle );
            }
        }else{
            $DirArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
        }
        return $DirArray;
    }
    //获取文件列表
    public function getFile( & $Dir ){
        if( is_dir($Dir) ){
            if( false != ($Handle = opendir($Dir)) ) {
                while( false != ($File = readdir($Handle)) ){
                    if( $File!='.' && $File!='..' && strpos($File,'.') ){
                        $FileArray[] = $File;
                    }
                }
                closedir( $Handle );
            }
        }else{
            $FileArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
        }
        return $FileArray;
    }
    //获取目录/文件列表
    public function getDirFile( & $Dir ){
        if( is_dir($Dir) ){
            $DirFileArray['DirList'] = $this->getDir( $Dir );
            if( $DirFileArray ){
                foreach( $DirFileArray['DirList'] as $Handle ){
                    $File = $Dir.DS.$Handle;
                    $DirFileArray['FileList'][$Handle] = $this->getFile( $File );
                }
            }
        }else{
            $DirFileArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
        }
        return $DirFileArray;
    }
}
?>

实例:(相对路径或绝对路径)

1.获取目录列表

<?php
$Dir_dir  = './example';
$getDirFile = new getDirFile();
$getDir = $getDirFile->getDir( $Dir_dir );
print_r($getDir);
?>

显示

<?php
$File_one_dir = './example/example_one';
$File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';
$getDirFile = new getDirFile();
$getFile_one = $getDirFile->getFile( $File_one_dir );
$getFile_two = $getDirFile->getFile( $File_two_dir );
print_r($getFile_one);
print_r($getFile_two);
?>

2.获取文件列表

<?php
$File_one_dir = './example/example_one';
$File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';
$getDirFile = new getDirFile();
$getFile_one = $getDirFile->getFile( $File_one_dir );
$getFile_two = $getDirFile->getFile( $File_two_dir );
print_r($getFile_one);
print_r($getFile_two);
?>

显示

Array
(
    [0] => example.sql
    [1] => example.txt
)
Array
(
    [0] => example.php
)

3.获取目录/文件列表

<?php
$Dir_dir  = './example';
$getDirFile = new getDirFile();
$getDirFile  = $getDirFile->getDirFile( $Dir_dir );
print_r($getDirFile);
?>

显示

Array
(
    [DirList] => Array
        (
            [0] => example_one
            [1] => example_two
        )
    [FileList] => Array
        (
            [example_one] => Array
                (
                    [0] => example.sql
                    [1] => example.txt
                )
            [example_two] => Array
                (
                    [0] => example.php
                )
        )
)
PHP 相关文章推荐
把从SQL中取出的数据转化成XMl格式
Oct 09 PHP
用PHP查询域名状态whois的类
Nov 25 PHP
在JavaScript中调用php程序
Mar 09 PHP
将文件夹压缩成zip文件的php代码
Dec 14 PHP
PHP XML操作类DOMDocument
Dec 16 PHP
用PHP为SHOPEX增加日志功能代码
Jul 02 PHP
解析php中memcache的应用
Jun 18 PHP
php实现查看邮件是否已被阅读的方法
Dec 03 PHP
浅析PHP7新功能及语法变化总结
Jun 17 PHP
Yii针对添加行的增删改查操作示例
Oct 18 PHP
Thinkphp5行为使用方法汇总
Dec 21 PHP
Laravel框架表单验证操作实例分析
Sep 30 PHP
php获取文件夹路径内的图片以及分页显示示例
Mar 11 #PHP
php上传图片存入数据库示例分享
Mar 11 #PHP
php使用反射插入对象示例分享
Mar 11 #PHP
php数组编码转换示例详解
Mar 11 #PHP
使用Discuz关键词服务器实现PHP中文分词
Mar 11 #PHP
PHP输出缓存ob系列函数详解
Mar 11 #PHP
php初始化对象和析构函数的简单实例
Mar 11 #PHP
You might like
PHP全概率运算函数(优化版) Webgame开发必备
2011/07/04 PHP
PHP自动载入类文件函数__autoload的使用方法
2019/03/25 PHP
Laravel框架下的Contracts契约详解
2020/03/17 PHP
javascript jQuery $.post $.ajax用法
2008/07/09 Javascript
基于jquery的文本框与autocomplete结合使用(asp.net+json)
2012/05/30 Javascript
js整数字符串转换为金额类型数据(示例代码)
2013/12/26 Javascript
jQuery+ajax中getJSON() 用法实例
2014/12/22 Javascript
jquery分析文本里url或邮件地址为真实链接的方法
2015/06/20 Javascript
javascript中select下拉框的用法总结
2016/01/07 Javascript
分享有关jQuery中animate、slide、fade等动画的连续触发、滞后反复执行的bug
2016/01/10 Javascript
JavaScript中获取时间的函数集
2016/08/16 Javascript
jquery获取table指定行和列的数据方法(当前选中行、列)
2016/11/07 Javascript
js输入框使用正则表达式校验输入内容的实例
2017/02/12 Javascript
bootstrap3使用bootstrap datetimepicker日期插件
2017/05/24 Javascript
vue-cli脚手架-bulid下的配置文件
2018/03/27 Javascript
详解vue指令与$nextTick 操作DOM的不同之处
2018/08/02 Javascript
js继承的这6种方式!(上)
2019/04/23 Javascript
jquery实现选项卡切换代码实例
2019/05/14 jQuery
vue3.0实现点击切换验证码(组件)及校验
2020/11/18 Vue.js
[02:38]2018年度DOTA2最佳劣单位选手-完美盛典
2018/12/17 DOTA
Python中isnumeric()方法的使用简介
2015/05/19 Python
利用python将xml文件解析成html文件的实现方法
2017/12/22 Python
TensorFlow实现AutoEncoder自编码器
2018/03/09 Python
Python Pandas 箱线图的实现
2019/07/23 Python
对tensorflow中cifar-10文档的Read操作详解
2020/02/10 Python
Python3利用scapy局域网实现自动多线程arp扫描功能
2021/01/21 Python
浅析数据存储的三种方式 cookie sessionstorage localstorage 的异同
2020/06/04 HTML / CSS
俄罗斯眼镜网: optikaworld
2016/07/31 全球购物
意大利大型购物中心:Oliviero.it
2017/10/19 全球购物
LN-CC美国:伦敦时尚生活的缩影
2019/02/19 全球购物
SOKOLOV官网:俄罗斯珠宝首饰品牌
2021/01/02 全球购物
党员干部群众路线个人整改措施
2014/09/18 职场文书
入党积极分子自我批评思想汇报
2014/10/10 职场文书
《叶问2》观后感
2015/06/15 职场文书
python编程项目中线上问题排查与解决
2021/11/01 Python
MySQL创建表操作命令分享
2022/03/25 MySQL