CoderZone.org

Category: >> PHP Code >> Remove All Files An A Given Directory Bookmark and Share

<< lastnext >>

Snippet Name: Remove All Files An A Given Directory

Description: This function will take a folder path and remove all files and folders below it. Use with caution!

Also see:
» Get the relative path to the root ...
» Write or update an htpasswd file i...
» Active Directory - Reading the Use...
» Validate file extensions in upload...
» Javascript Include - allows dynami...
» Remove empty lines from a string o...
» Automatically find location of jav...
» Remove empty lines from text files
» Simple directory viewer / browser
» Recursive Directory Browser
» Delete a file in Visual Basic
» Copy a folder in Visual Basic
» Copy a file in VB
» Reading and Writing to text files ...
» Parsing PHP in Javascript Files
» Perl version of file_get_contents&...
» Read the contents of an entire fil...
» Create an array from the contents ...
» Create an array from the lines of ...
» Remove Duplicate Lines from a File
» Extract Just The BODY Of An HTML F...
» Rename Files Interactively
» Sort A File List By Month Name In ...
» Get File Size Over HTTP
» Get A Directory Listing Of Specifi...
» Check If A Directory Allows File C...
» Copy Files By Date Modified
» Output To STDOUT Or A File
» Include All Files In A Directory
» More File Type Detection in PHP

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: February 27th, 2009

 
<?php
 
function emptyDir($path) { 
 
     // init the debug string
     $debugStr = '';
     $debugStr .= "Deleting Contents Of: $path<br /><br />";
 
     // parse the folder
     if ($handle = opendir($path)) {
 
          while (false !== ($file = readdir($handle))) {
 
               if ($file != "." && $file != "..") {
 
               // If it's a file, delete it
               if(is_file($path."/".$file)) {
 
                    if(unlink($path."/".$file)) {
                    $debugStr .= "Deleted File: ".$file."<br />";     
                    }
 
               } else {
 
                    // It's a directory...
                    // crawl through the directory and delete the contents               
                    if($handle2 = opendir($path."/".$file)) {
 
                         while (false !== ($file2 = readdir($handle2))) {
 
                              if ($file2 != "." && $file2 != "..") {
                                   if(unlink($path."/".$file."/".$file2)) {
                                   $debugStr .= "Deleted File: $file/$file2<br />";     
                                   }
                              }
 
                         }
 
                    }
 
                    if(rmdir($path."/".$file)) {
                    $debugStr .= "Directory: ".$file."<br />";     
                    }
 
               }
 
               }
 
          }
 
     }
     return $debugStr;
}
 
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 4