日期:2015-06-28 00:00:00 来源: IT猫扑网
在面试中我们经常遇到这个题目: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(
相关文章
相关下载
网友评论
我要评论...