PHP implements recursive loop of every Directory Program

Source: Internet
Author: User
Tags parent directory
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 );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.