PHP实现递归目录的5种方法


Posted in PHP onOctober 27, 2016

项目开发中免不了要在服务器上创建文件夹,比如上传图片时的目录,模板解析时的目录等。这不当前手下的项目就用到了这个,于是总结了几个循环创建目录的方法。

方法一:使用glob循环

<?php
//方法一:使用glob循环
 
function myscandir1($path, &$arr) {
 
  foreach (glob($path) as $file) {
    if (is_dir($file)) {
      myscandir1($file . '/*', $arr);
    } else {
 
      $arr[] = realpath($file);
    }
  }
}
?>

方法二:使用dir && read循环

<?php
//方法二:使用dir && read循环
function myscandir2($path, &$arr) {
 
  $dir_handle = dir($path);
  while (($file = $dir_handle->read()) !== false) {
 
    $p = realpath($path . '/' . $file);
    if ($file != "." && $file != "..") {
      $arr[] = $p;
    }
 
    if (is_dir($p) && $file != "." && $file != "..") {
      myscandir2($p, $arr);
    }
  }
}
?>

方法三:使用opendir && readdir循环

<?php
//方法三:使用opendir && readdir循环
function myscandir3($path, &$arr) {
   
  $dir_handle = opendir($path);
  while (($file = readdir($dir_handle)) !== false) {
 
    $p = realpath($path . '/' . $file);
    if ($file != "." && $file != "..") {
      $arr[] = $p;
    }
    if (is_dir($p) && $file != "." && $file != "..") {
      myscandir3($p, $arr);
    }
  }
}
 ?>

 方法四:使用scandir循环
 

<?php
//方法四:使用scandir循环
function myscandir4($path, &$arr) {
   
  $dir_handle = scandir($path);
  foreach ($dir_handle as $file) {
 
    $p = realpath($path . '/' . $file);
    if ($file != "." && $file != "..") {
      $arr[] = $p;
    }
    if (is_dir($p) && $file != "." && $file != "..") {
      myscandir4($p, $arr);
    }
  }
}
 ?>

方法五:使用SPL循环

<?php
//方法五:使用SPL循环
function myscandir5($path, &$arr) {
 
  $iterator = new DirectoryIterator($path);
  foreach ($iterator as $fileinfo) {
 
    $file = $fileinfo->getFilename();
    $p = realpath($path . '/' . $file);
    if (!$fileinfo->isDot()) {
      $arr[] = $p;
    }
    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
      myscandir5($p, $arr);
    }
  }
}
?>

 可以用xdebug测试运行时间

<?php
myscandir1('./Code',$arr1);//0.164010047913 
myscandir2('./Code',$arr2);//0.243014097214 
myscandir3('./Code',$arr3);//0.233012914658 
myscandir4('./Code',$arr4);//0.240014076233
myscandir5('./Code',$arr5);//0.329999923706
 
 
//需要安装xdebug
echo xdebug_time_index(), "\n";
?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
smarty静态实验表明,网络上是错的~呵呵
Nov 25 PHP
PHP中CURL方法curl_setopt()函数的参数分享
Jan 19 PHP
如何使用PHP批量去除文件UTF8 BOM信息
Aug 05 PHP
thinkphp的URL路由规则与配置实例
Nov 26 PHP
php接口数据加密、解密、验证签名
Mar 12 PHP
PHP使用ffmpeg给视频增加字幕显示的方法
Mar 12 PHP
为百度UE编辑器上传图片添加水印功能
Apr 16 PHP
深入讲解PHP的Yii框架中的属性(Property)
Mar 18 PHP
Zend Framework常用校验器详解
Dec 09 PHP
Docker配置PHP开发环境教程
Dec 21 PHP
PHP chunk_split()函数讲解
Feb 12 PHP
PHP数组实际占用内存大小原理解析
Dec 11 PHP
PHP读取大文件的几种方法介绍
Oct 27 #PHP
php array_multisort 对数组进行排序详解及实例代码
Oct 27 #PHP
PHP中的密码加密的解决方案总结
Oct 26 #PHP
php 解析xml 的四种方法详细介绍
Oct 26 #PHP
PHP 以POST方式提交XML、获取XML,解析XML详解及实例
Oct 26 #PHP
php 生成签名及验证签名详解
Oct 26 #PHP
PHP XML和数组互相转换详解
Oct 26 #PHP
You might like
php使用NumberFormatter格式化货币的方法
2015/03/21 PHP
php curl 上传文件代码实例
2015/04/27 PHP
php微信开发之带参数二维码的使用
2016/08/03 PHP
php文件后缀不强制为.php的实操方法
2019/09/18 PHP
简单的jquery拖拽排序效果实现代码
2011/09/20 Javascript
简介JavaScript中getUTCMonth()方法的使用
2015/06/10 Javascript
Bootstrap3制作搜索框样式的方法
2016/07/11 Javascript
Bootstrap Table使用方法解析
2016/10/19 Javascript
AngularJS过滤器filter用法总结
2016/12/13 Javascript
JS实现物体带缓冲的间歇运动效果示例
2016/12/22 Javascript
jQuery页面弹出框实现文件上传
2017/02/09 Javascript
微信小程序嵌入腾讯视频源过程详解
2019/08/08 Javascript
vue使用echarts画组织结构图
2021/02/06 Vue.js
[02:08]2014DOTA2国际邀请赛 430专访:力争取得小组前二
2014/07/11 DOTA
Python multiprocessing模块中的Pipe管道使用实例
2015/04/11 Python
python3爬虫怎样构建请求header
2018/12/23 Python
在Python 不同级目录之间模块的调用方法
2019/01/19 Python
python flask框架实现重定向功能示例
2019/07/02 Python
Pyinstaller 打包exe教程及问题解决
2019/08/16 Python
python不相等的两个字符串的 if 条件判断为True详解
2020/03/12 Python
python使用pyecharts库画地图数据可视化的实现
2020/03/25 Python
Python爬取YY评级分数并保存数据实现过程解析
2020/06/01 Python
python如何爬取动态网站
2020/09/09 Python
Python GUI之tkinter窗口视窗教程大集合(推荐)
2020/10/20 Python
python 解决Windows平台上路径有空格的问题
2020/11/10 Python
为什么要使用servlet
2016/01/17 面试题
婚礼证婚人证婚词
2014/01/08 职场文书
给老师的道歉信
2014/01/11 职场文书
初三学生个人自我评定
2014/04/06 职场文书
大学生党员个人对照检查材料范文
2014/09/25 职场文书
2014年度工作总结报告
2014/12/15 职场文书
清洁员岗位职责
2015/02/15 职场文书
三傻大闹宝莱坞观后感
2015/06/03 职场文书
春晚观后感
2015/06/11 职场文书
带你彻底理解JavaScript中的原型对象
2021/04/14 Javascript
mysql sum(if())和count(if())的用法说明
2022/01/18 MySQL