|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 |
//第一种 遍历放入数据中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
traverse($path
= ‘.‘) { $current_dir
= opendir ( $path
); //opendir()返回一个目录句柄,失败返回false while
( ($file
= readdir ( $current_dir
)) !== false ) { //readdir()返回打开目录句柄中的一个条目 $sub_dir
= $path
. DIRECTORY_SEPARATOR . $file; //构建子目录路径 if
($file
== ‘.‘
|| $file
== ‘..‘) { continue; } else
if
(is_dir
( $sub_dir
)) { //如果是目录,进行递归 echo
‘文件目录 ‘
. $file
. ‘:<br>‘; traverse ( $sub_dir
); } else
{ //如果是文件,直接输出 echo
‘文件路径‘
. $path
. ‘: ‘
. $file
. ‘<br>‘; } }} |
php遍历文件夹下的所有文件及文件夹,布布扣,bubuko.com
原文:http://www.cnblogs.com/lixiuran/p/3614816.html