在面试中我们经常遇到这个题目:php遍历一个文件夹下的所有文件和子文件夹。
这个题目有好多种解决方法。但大致思路都一样。采用递归。
- $path = './filepath';
- function getfiles($path)
- {
- if(!is_dir($path)) return;
- $handle = opendir($path);
- while( false !== ($file = readdir($handle)))
- {
- if($file != '.' && $file!='..')
- {
- $path2= $path.'/'.$file;
- if(is_dir($path2))
- {
- echo '
- ';
- echo $file;
- getfiles($path2);
- }else
- {
- echo '
- ';
- echo $file;
- }
- }
- }
- }
- print_r( getfiles($path));
- echo '
';- function getdir($path)
- {
- if(!is_dir($path)) return;
- $handle = dir($path);
- while($file=$handle->read())
- {
- if($file!='.' && $file!='..')
- {
- $path2 = $path.'/'.$file;
- if(is_dir($path2))
- {
- echo $file."\t";
- getdir($path2);
- }else
- {
- echo $file.'
- ';
- }
- }
- }
- }
- getdir($path);
- echo '
';- function get_dir_scandir(关键词标签:php
相关阅读
热门文章
plsql developer怎么连接数据库-plsql developer连接数据库方法
2021年最好用的10款php开发工具推荐
php利用淘宝IP库获取用户ip地理位置
在 PHP 中使用命令行工具
人气排行 详解ucenter原理及第三方应用程序整合思路、方法 plsql developer怎么连接数据库-plsql developer连接数据库方法 PHP中防止SQL注入攻击 PHP会话Session的具体使用方法解析 PHP运行出现Notice : Use of undefined constant 的解决办法 PHP如何清空mySQL数据库 CakePHP程序员必须知道的21条技巧 PHP采集图片实例(PHP采集)
查看所有1条评论>>