php遍历文件夹下的所有文件和子文件夹示例


Posted in PHP onMarch 20, 2014

遍历目录,结果存入数组。支持php4及以上。php5以后可用scandir()函数代替while循环。

<?php
/**
* @param string $dir
* @return array
*/
function my_scandir($dir)
{
 $files = array();
 if ( $handle = opendir($dir) ) {
  while ( ($file = readdir($handle)) !== false ) 
  {
   if ( $file != ".." && $file != "." ) 
   {
    if ( is_dir($dir . "/" . $file) ) 
    {
     $files[$file] = my_scandir($dir . "/" . $file);
    }
    else 
    {
     $files[] = $file;
    }
   }
  }
  closedir($handle);
  return $files;
 }
}
function my_scandir1($dir)
{
 $files = array();
 $dir_list = scandir($dir);
 foreach($dir_list as $file)
 {
  if ( $file != ".." && $file != "." ) 
  {
   if ( is_dir($dir . "/" . $file) ) 
   {
    $files[$file] = my_scandir1($dir . "/" . $file);
   }
   else 
   {
    $files[] = $file;
   }
  }
 }
 return $files;
}
$result = my_scandir('./');
$result = my_scandir1('./');
?>

另一个实现方法

function fetchDir($dir) { 
        foreach(glob($dir.'\*') as $file) { 
            echo $file,"\n"; 
            if(is_dir($file)) { 
                fetchDir($file); 
            } 
        } 
    } 
    fetchDir("D:\wamp\www\any");

还可以把‘\*', 换成 DIRECTORY_SEPARATOR.'*' ,把‘\n'换成PHP_EOL ,这样可以跨平台了。

PHP 相关文章推荐
php中ob(Output Buffer 输出缓冲)函数使用方法
Jul 21 PHP
php文档更新介绍
Jul 22 PHP
将酷狗krc歌词解析并转换为lrc歌词php源码
Jun 20 PHP
ThinkPHP中I(),U(),$this-&gt;post()等函数用法
Nov 22 PHP
linux下实现定时执行php脚本
Feb 13 PHP
THINKPHP支持YAML配置文件的设置方法
Mar 17 PHP
PHP下SSL加密解密、验证、签名方法(很简单)
Jun 28 PHP
ThinkPHP中Common/common.php文件常用函数功能分析
May 20 PHP
linux下php上传文件注意事项
Jun 11 PHP
PHP获取表单数据与HTML嵌入PHP脚本的实现
Feb 09 PHP
PHP使用finfo_file()函数检测上传图片类型的实现方法
Apr 18 PHP
PHP面向对象类型约束用法分析
Jun 12 PHP
php数据库备份还原类分享
Mar 20 #PHP
PHP实现微信公众平台音乐点播
Mar 20 #PHP
一个PHP针对数字的加密解密类
Mar 20 #PHP
php的array数组和使用实例简明教程(容易理解)
Mar 20 #PHP
PHP引用(&amp;)各种使用方法实例详解
Mar 20 #PHP
PHP使用imagick读取PDF生成png缩略图的两种方法
Mar 20 #PHP
递归删除一个节点以及该节点下的所有节点示例
Mar 19 #PHP
You might like
二十行语句实现从Excel到mysql的转化
2006/10/09 PHP
php数组的概述及分类与声明代码演示
2013/02/26 PHP
浅谈socket同步和异步、阻塞和非阻塞、I/O模型
2016/12/15 PHP
php使用PDO获取结果集的方法
2017/02/16 PHP
PHP实现的装箱算法示例
2018/06/23 PHP
PHP实现会员账号单唯一登录的方法分析
2019/03/07 PHP
jQuery帮助之CSS尺寸(五)outerHeight、outerWidth
2009/11/14 Javascript
关于图片的预加载过程中隐藏未知的
2012/12/19 Javascript
javascript实现的元素拖动函数宿主为浏览器
2014/07/21 Javascript
文本框倒叙输入让输入框的焦点始终在最开始的位置
2014/09/01 Javascript
js+HTML5实现视频截图的方法
2015/06/16 Javascript
Javascript中常用的检测方法小结
2016/10/08 Javascript
jquery Ajax实现Select动态添加数据
2017/06/08 jQuery
Vue项目实现换肤功能的一种方案分析
2019/08/28 Javascript
微信小程序实现抖音播放效果的实例代码
2020/04/11 Javascript
jQuery实现移动端笔触canvas电子签名
2020/05/21 jQuery
Vue跨域请求问题解决方案过程解析
2020/08/07 Javascript
antd Select下拉菜单动态添加option里的内容操作
2020/11/02 Javascript
详解Vue中的自定义指令
2020/12/07 Vue.js
下载给定网页上图片的方法
2014/02/18 Python
python通过get,post方式发送http请求和接收http响应的方法
2015/05/26 Python
python字符串循环左移
2019/03/08 Python
检测python爬虫时是否代理ip伪装成功的方法
2019/07/12 Python
Python实现桌面翻译工具【新手必学】
2020/02/12 Python
Python实现区域填充的示例代码
2021/02/03 Python
细说CSS3中box属性中的overflow-x属性和overflow-y属性值的效果
2014/07/21 HTML / CSS
英国建筑用品在线:Building Supplies Online(BSO)
2018/04/30 全球购物
中国茶叶、茶具一站式网上购物商城:醉品茶城
2018/07/03 全球购物
Cecil Mode法国在线商店:女性时尚
2021/01/08 全球购物
销售高级职员求职信
2013/10/29 职场文书
外贸实习生自荐信范文
2013/11/24 职场文书
亮化工程实施方案
2014/03/17 职场文书
婚礼领导致辞大全
2015/07/28 职场文书
python非标准时间的转换
2021/07/25 Python
Redis如何实现验证码发送 以及限制每日发送次数
2022/04/18 Redis