php生成目录树

原创
2013/06/14 01:40
阅读数 2.4K
<?php
$dir = 'E:\AppServ\www\alpaca\app';

function recurDir($pathName) {
	$result = array();
	$tmp = array();
	if( !is_dir($pathName) || !is_readable($pathName) ){
		return null;
	}
	$allFiles = scandir($pathName);
	foreach($allFiles as $fileName){
		if( in_array($fileName, array('.', '..')) ) continue;
		$fullName = $pathName . '/' . $fileName;
		if( is_dir($fullName) ){
			$result[$fileName] = recurDir($fullName);
		}else{
			$temp[] = $fileName;
		}
	}
	if($temp){
		foreach( $temp as $f ){
			$result[] = $f;
		}
	}
	
	return $result;
}


function bl($arr, $l = '-|'){
	static $l = '';
	static $str = '';
	foreach($arr as $key=>$val){
		if(is_array($arr[$key])){
			//echo $val . "<br>";
			//echo $l . $key . "<br>";
			$str .= $l . $key . "<br>";
			$l .= '-|';
			bl($arr[$key], $l);
		}else{
			//echo $l . $val . "<br>";
			$str .= $l . $val . "<br>";
		}
	}
	$l = '';
	return $str;
}


$tree = recurDir($dir);
echo "<pre>";
print_r($tree);
echo "</pre>";
echo "<br>------------------------------------------<br>";
$data = bl($tree);
echo "<pre>";
print_r($data);
echo "</pre>";

下面都是预览

Array
(
    [c] => Array
        (
            [admin] => Array
                (
                    [0] => __construct.php
                    [1] => account.php
                    [2] => elem.php
                    [3] => home.php
                    [4] => model.php
                    [5] => page.php
                    [6] => plugin.php
                    [7] => setting.php
                    [8] => template.php
                    [9] => user.php
                )

            [0] => __construct.php
            [1] => file.php
            [2] => home.php
            [3] => page.php
            [4] => plugin.php
            [5] => user.php
        )

    [cache] => Array
        (
        )

    [db] => Array
        (
            [0] => 20120623234417.sql
            [1] => 20120624005854.sql
        )

    [lib] => Array
        (
            [0] => img.php
            [1] => utility.php
        )

    [m] => Array
        (
            [0] => elem_m.php
            [1] => user_m.php
        )

    [plugin] => Array
        (
            [freeform] => Array
                (
                    [c] => Array
                        (
                            [admin] => Array
                                (
                                    [0] => home.php
                                )

                            [0] => home.php
                        )

                    [v] => Array
                        (
                            [admin] => Array
                                (
                                    [form] => Array
                                        (
                                            [0] => add.php
                                            [1] => list-table.php
                                            [2] => list.php
                                            [3] => show.php
                                        )

                                )

                        )

                )

        )

    [tmp] => Array
        (
            [0] => feedback.php
            [1] => home.php
            [2] => layout.php
            [3] => list.php
            [4] => list_product.php
            [5] => product_list.php
            [6] => product_view.php
            [7] => tag.php
            [8] => template.php
        )

    [v] => Array
        (
            [admin] => Array
                (
                    [layout] => Array
                        (
                            [0] => add.php
                            [1] => list-table.php
                            [2] => list.php
                            [3] => show.php
                        )

                    [link] => Array
                        (
                            [0] => add.php
                            [1] => list-table.php
                            [2] => list.php
                            [3] => show.php
                        )

                    [model] => Array
                        (
                            [0] => add.php
                            [1] => list-table.php
                        )

                    [page] => Array
                        (
                            [0] => add.php
                            [1] => list-table.php
                        )

                    [plugin] => Array
                        (
                            [0] => index.php
                        )

                    [setting] => Array
                        (
                            [0] => add.php
                            [1] => backup.php
                            [2] => label.php
                        )

                    [template] => Array
                        (
                            [0] => add.php
                            [1] => dir.php
                            [2] => file.php
                            [3] => list-table.php
                        )

                    [user] => Array
                        (
                            [0] => account.php
                            [1] => add.php
                            [2] => list-table.php
                            [3] => list.php
                            [4] => login.php
                            [5] => show.php
                        )

                    [0] => home.php
                    [1] => temp.php
                )

            [elem] => Array
                (
                    [0] => add.php
                    [1] => list-table.php
                    [2] => list.php
                    [3] => page.php
                    [4] => post.php
                    [5] => show.php
                )

            [home] => Array
                (
                    [0] => index.php
                    [1] => install.php
                )

            [user] => Array
                (
                    [0] => add.php
                    [1] => list-table.php
                    [2] => list.php
                    [3] => login.php
                    [4] => show.php
                )

            [0] => 404.php
            [1] => redirect.php
        )

    [0] => b2core.php
    [1] => config.php
    [2] => config_app.php
    [3] => config_user.php
    [4] => mysql_ins.sql
    [5] => sqlite_ins.sql
)

------------------------------------------
c
-|admin
-|-|__construct.php
-|-|account.php
-|-|elem.php
-|-|home.php
-|-|model.php
-|-|page.php
-|-|plugin.php
-|-|setting.php
-|-|template.php
-|-|user.php
__construct.php
file.php
home.php
page.php
plugin.php
user.php
cache
db
-|20120623234417.sql
-|20120624005854.sql
lib
-|img.php
-|utility.php
m
-|elem_m.php
-|user_m.php
plugin
-|freeform
-|-|c
-|-|-|admin
-|-|-|-|home.php
home.php
v
-|admin
-|-|form
-|-|-|add.php
-|-|-|list-table.php
-|-|-|list.php
-|-|-|show.php
tmp
-|feedback.php
-|home.php
-|layout.php
-|list.php
-|list_product.php
-|product_list.php
-|product_view.php
-|tag.php
-|template.php
v
-|admin
-|-|layout
-|-|-|add.php
-|-|-|list-table.php
-|-|-|list.php
-|-|-|show.php
link
-|add.php
-|list-table.php
-|list.php
-|show.php
model
-|add.php
-|list-table.php
page
-|add.php
-|list-table.php
plugin
-|index.php
setting
-|add.php
-|backup.php
-|label.php
template
-|add.php
-|dir.php
-|file.php
-|list-table.php
user
-|account.php
-|add.php
-|list-table.php
-|list.php
-|login.php
-|show.php
home.php
temp.php
elem
-|add.php
-|list-table.php
-|list.php
-|page.php
-|post.php
-|show.php
home
-|index.php
-|install.php
user
-|add.php
-|list-table.php
-|list.php
-|login.php
-|show.php
404.php
redirect.php
b2core.php
config.php
config_app.php
config_user.php
mysql_ins.sql
sqlite_ins.sql








展开阅读全文
打赏
0
9 收藏
分享
加载中
更多评论
打赏
0 评论
9 收藏
0
分享
返回顶部
顶部