PHP遍历文件夹下子目录和文件

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
<?php

function read_all_dir ( $dir )
{
$result = array();
$handle = opendir($dir);
if ( $handle )
{
while ( ( $file = readdir ( $handle ) ) !== false )
{
if ( $file != '.' && $file != '..')
{
$cur_path = $dir . DIRECTORY_SEPARATOR . $file;
if ( is_dir ( $cur_path ) )
{
$result['dir'][$cur_path] = read_all_dir ( $cur_path );
}
else
{
$result['file'][] = $cur_path;
}
}
}
closedir($handle);
}
return $result;
}

$result = read_all_dir('/home/www/hexo');
var_dump($result);

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
print:

array(2) {
["file"]=>
array(4) {
[0]=>
string(27) "/home/www/hexo/package.json"
[1]=>
string(22) "/home/www/hexo/db.json"
[2]=>
string(25) "/home/www/hexo/.npmignore"
[3]=>
string(26) "/home/www/hexo/_config.yml"
}
["dir"]=>
array(7) {
["/home/www/hexo/.idea"]=>
array(2) {
["file"]=>
array(7) {
[0]=>
string(34) "/home/www/hexo/.idea/encodings.xml"
[1]=>
string(30) "/home/www/hexo/.idea/blade.xml"
[2]=>
string(42) "/home/www/hexo/.idea/jsLibraryMappings.xml"
[3]=>
string(34) "/home/www/hexo/.idea/workspace.xml"
[4]=>
string(29) "/home/www/hexo/.idea/hexo.iml"
[5]=>
string(29) "/home/www/hexo/.idea/misc.xml"
[6]=>
string(32) "/home/www/hexo/.idea/modules.xml"
}
["dir"]=>
array(2) {
["/home/www/hexo/.idea/libraries"]=>
array(1) {
["file"]=>
array(1) {
[0]=>
string(52) "/home/www/hexo/.idea/libraries/hexo_node_modules.xml"
}
}
["/home/www/hexo/.idea/copyright"]=>
array(1) {
["file"]=>
array(1) {
[0]=>
string(52) "/home/www/hexo/.idea/copyright/profiles_settings.xml"
}
}
}
}
... ...

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!