The code is as follows: |
Copy code |
<? Php Class listdir { Var $ depth; Var $ dirname; Var $ list; Var $ tostring; Function listdir ($ dir ){ $ This-> dirname = $ dir; $ This-> depth = 0; $ This-> tostring = ""; } // Save the result to a multi-dimensional array Function getlist ($ dir = ""){ If ($ dir = "") $ dir = $ this-> dirname; $ D = @ dir ($ dir ); While (false! ==( $ Item = $ d-> read ())) { If ($ item! = "." & $ Item! = "..") { $ Path = $ dir. "/". $ item; If (is_dir ($ path )){ $ This-> depth + = 1; $ This-> getlist ($ path ); } Else { $ This-> list [$ this-> depth] [] = $ item; } } } $ This-> list [$ this-> depth] ['Directory'] = $ dir; $ This-> depth-= 1; $ D-> close (); Return $ this-> list; } // Escape results Function tostring ($ dir = ""){ If ($ dir = "") $ dir = $ this-> dirname; $ D = @ dir ($ dir ); $ This-> tostring. = "<UL> n "; $ This-> tostring. = "Directory:". $ dir. "n "; While (false! ==( $ Item = $ d-> read ())) { If ($ item! = "." & $ Item! = "..") { $ Path = $ dir. "/". $ item; If (is_dir ($ path )){ $ This-> depth + = 1; $ This-> tostring ($ path ); } Else { $ This-> tostring. = "<LI>". $ item. "</LI> n "; } } } $ This-> depth-= 1; $ D-> close (); $ This-> tostring. = "</UL> n "; Return $ this-> tostring; } } $ Wapdir = "jquery "; $ D = new listdir ($ wapdir ); Echo $ d-> tostring (); ?> |
Recursively create multi-level directories
1. first, determine whether the bottom-layer directory div/css/layout exists. 2. determine whether the upper-level directory div/css of the div/css/layout exists. If it does not exist, use div/css as the parameter for recursion.
The following is the program code:
The code is as follows: |
Copy code |
Function mkdirs ($ dir) { If (! Is_dir ($ dir )) { If (! Mkdirs (dirname ($ dir ))){ Return false; } If (! Mkdir ($ dir, 0777 )){ Return false; } } Return true; } Mkdirs ('div/css/layout '); In the same way, php uses rmdir and unlink to recursively delete code for multi-level directories:
Function rmdirs ($ dir) { $ D = dir ($ dir ); While (false! ==( $ Child = $ d-> read ())){ If ($ child! = '.' & $ Child! = '..'){ If (is_dir ($ dir. '/'. $ child )) Rmdirs ($ dir. '/'. $ child ); Else unlink ($ dir. '/'. $ child ); } } $ D-> close (); Rmdir ($ dir ); } |