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 相关文章推荐
PHP STRING 陷阱原理说明
Jul 24 PHP
Windows 下的 PHP-PEAR 安装方法
Nov 20 PHP
php设计模式 Delegation(委托模式)
Jun 26 PHP
CI框架源码阅读,系统常量文件constants.php的配置
Feb 28 PHP
php错误级别的设置方法
Jun 17 PHP
解析php 版获取重定向后的地址(代码)
Jun 26 PHP
PHP实现仿百度文库,豆丁在线文档效果(word,excel,ppt转flash)
Mar 10 PHP
Zend Framework教程之Zend_Config_Ini用法分析
Mar 23 PHP
PHP+Jquery与ajax相结合实现下拉淡出瀑布流效果【无需插件】
May 06 PHP
php网页版聊天软件实现代码
Aug 12 PHP
laravel-admin表单提交隐藏一些数据,回调时获取数据的方法
Oct 08 PHP
laravel 解决后端无法获取到前端Post过来的值问题
Oct 22 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
全文搜索和替换
2006/10/09 PHP
php 计算两个时间戳相隔的时间的函数(小时)
2009/12/18 PHP
PHP中把对象数组转换成普通数组的方法
2015/07/10 PHP
PHP怎样用正则抓取页面中的网址
2016/08/09 PHP
基于PHP实现栈数据结构和括号匹配算法示例
2017/08/10 PHP
有道JavaScript监听浏览器的问题
2010/06/23 Javascript
Javascript 面试题随笔
2011/03/31 Javascript
jqGrid jQuery 表格插件测试代码
2011/08/23 Javascript
纯JS实现动态时间显示代码
2014/02/08 Javascript
webpack实现热加载自动刷新的方法
2017/07/30 Javascript
微信小程序中吸底按钮适配iPhone X方案
2017/11/29 Javascript
Vue+ElementUI table实现表格分页
2019/12/14 Javascript
Vue强制组件重新渲染的方法讨论
2020/02/03 Javascript
Python循环语句中else的用法总结
2016/09/11 Python
python与php实现分割文件代码
2017/03/06 Python
Python cookbook(数据结构与算法)实现对不原生支持比较操作的对象排序算法示例
2018/03/15 Python
从请求到响应过程中django都做了哪些处理
2018/08/01 Python
10 分钟快速入门 Python3的教程
2019/01/29 Python
python如何实现数据的线性拟合
2019/07/19 Python
Python generator生成器和yield表达式详解
2019/08/08 Python
浅谈Python中range与Numpy中arange的比较
2020/03/11 Python
在python中实现求输出1-3+5-7+9-......101的和
2020/04/02 Python
Python接收手机短信的代码整理
2020/08/02 Python
使用HTML5拍照示例代码
2013/08/06 HTML / CSS
卫校护理专业毕业生求职信
2013/11/26 职场文书
公司接待方案
2014/03/08 职场文书
农村改厕实施方案
2014/03/22 职场文书
体现团队精神的口号
2014/06/06 职场文书
化学教育专业自荐信
2014/07/04 职场文书
贷款承诺书
2015/01/20 职场文书
五一劳动节活动总结
2015/02/09 职场文书
2015年六一儿童节演讲稿
2015/03/19 职场文书
湘江北去观后感
2015/06/15 职场文书
离婚协议书格式范本
2016/03/18 职场文书
Go语言实现Snowflake雪花算法
2021/06/08 Golang
详解MongoDB的条件查询和排序
2021/06/23 MongoDB