PHP Delete a Directory/Folder?

  • Funny_Fuzz
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1517

Post 3+ Months Ago

Is there a script that will make PHP delete a specified folder/directory?
Thanks in Advance! :D
  • placid psychosis
  • Proficient
  • Proficient
  • User avatar
  • Posts: 284
  • Loc: Warsaw, IN

Post 3+ Months Ago

Yes...

PHP Code: [ Select ]
function delete_directory($dirname) {
   if (is_dir($dirname))
      $dir_handle = opendir($dirname);
   if (!$dir_handle)
      return false;
   while($file = readdir($dir_handle)) {
      if ($file != "." && $file != "..") {
         if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
         else
            delete_directory($dirname.'/'.$file);    
      }
   }
   closedir($dir_handle);
   rmdir($dirname);
   return true;
}
 
  1. function delete_directory($dirname) {
  2.    if (is_dir($dirname))
  3.       $dir_handle = opendir($dirname);
  4.    if (!$dir_handle)
  5.       return false;
  6.    while($file = readdir($dir_handle)) {
  7.       if ($file != "." && $file != "..") {
  8.          if (!is_dir($dirname."/".$file))
  9.             unlink($dirname."/".$file);
  10.          else
  11.             delete_directory($dirname.'/'.$file);    
  12.       }
  13.    }
  14.    closedir($dir_handle);
  15.    rmdir($dirname);
  16.    return true;
  17. }
  18.  


This code can easily be improved upon, as it's a quick hack, but it takes a directory as an argument and then uses functional recursion to delete all files and folders within, and then finally removes the directory. Nice and quick, too.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 32 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2015. Ozzu® is a registered trademark of Unmelted, LLC.