Php deletes non-empty Directory user-defined functions

Source: Internet
Author: User

// Cyclically delete directories and file functions

The code is as follows: Copy code

Function delDirAndFile ($ dirName)
{
If ($ handle = opendir ("$ dirName ")){
While (false! ==( $ Item = readdir ($ handle ))){
If ($ item! = "." & $ Item! = "...") {
If (is_dir ("$ dirName/$ item ")){
DelDirAndFile ("$ dirName/$ item ");
} Else {
If (unlink ("$ dirName/$ item") echo "the file is successfully deleted: $ dirName/$ item
N ";
}
}
}
Closedir ($ handle );
If (rmdir ($ dirName) echo "the directory is successfully deleted: $ dirName
N ";
}
}
?>

// All objects in the loop Directory

The code is as follows: Copy code

Function delFileUnderDir ($ dirName = "../Smarty/templates/templates_c ")
{
If ($ handle = opendir ("$ dirName ")){
While (false! ==( $ Item = readdir ($ handle ))){
If ($ item! = "." & $ Item! = "...") {
If (is_dir ("$ dirName/$ item ")){
DelFileUnderDir ("$ dirName/$ item ");
} Else {
If (unlink ("$ dirName/$ item") echo "the file is successfully deleted: $ dirName/$ item
N ";
}
}
}
Closedir ($ handle );
}
}

?>

Directory deletion function

The code is as follows: Copy code

Function my_del_dir ($ directory)


    { 
If (is_dir ($ directory) = false)
        { 
Exit ("The Directory Is Not Exist! ");
        } 
$ Handle = opendir ($ directory );
While ($ file = readdir ($ handle ))! = False)
        { 
If ($ file! = "." & $ File! = "..")
            { 
Is_dir ("$ directory/$ file ")?
My_del_dir ("$ directory/$ file "):
Unlink ("$ directory/$ file ");
            } 
        } 
If (readdir ($ handle) = false)
        { 
Closedir ($ handle );
Rmdir ($ directory );
        } 
    } 
?>

Write your own


$ SourceDir directory to be deleted

Call DeleteDir ("images/abc"); // delete the abc Directory

The code is as follows: Copy code

Function DeleteDir ($ sourceDir ){
$ Succeed = true;
If (file_exists ($ sourceDir )){
$ ObjDir = opendir ($ sourceDir );
While (false! ==( $ FileName = readdir ($ objDir ))){
If ($ fileName! = ".") & ($ FileName! = "...") {
Chmod ("$ sourceDir/$ fileName", 0777 );
If (! Is_dir ("$ sourceDir/$ fileName ")){
If (! Unlink ("$ sourceDir/$ fileName ")){
$ Succeed = false;
Break;
}
}
Else {
DeleteDir ("$ sourceDir/$ fileName ");
}
}
}
If (! Readdir ($ objDir )){
Closedir ($ objDir );
If (! Rmdir ($ sourceDir )){
$ Succeed = false;
}
}
}
Return $ succeed;
}

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.