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 (); ?> |
It's easy to delete an empty directory ~ One
You can do this by using the rmdir () function. However, to delete a non-empty directory, you cannot quickly delete it. You must first delete the files in the directory, however, there may be subdirectories in the directory, so recursive deletion is required ~ The following is my example ~
The code is as follows: |
Copy code |
<? Php Function deletedir ($ dir ){ If (! Handle = @ opendir ($ dir) {// check whether the directory to be opened exists Die ("this directory is not available "); } While (false! ==( $ File = readdir ($ handle ))){ If ($ file! = "." & $ File! = "..") {// Exclude the current directory and parent directory $ File = $ dir. DIRECTORY_SEPARATOR. $ file; If (is_dir ($ file )){ Deletedir ($ file ); } Else { If (@ unlink ($ file )){ Echo "file <B> $ file </B> is successfully deleted. <Br> "; } Else { Echo "file <B> $ file </B> failed to be deleted! <Br> "; } } } If (@ rmdir ($ dir )){ Echo "directory <B> $ dir </B> is successfully deleted. <Br> n "; } Else { Echo "directory <B> $ dir </B> failed to be deleted! <Br> n "; } } // Test the program $ Dir = "/var/www/test "; Deletedir ($ dir ); ?> |