Define recursive function to delete directories : Function Recursion « Functions « PHP






Define recursive function to delete directories


<?php

   function delete_directory($dir) {
      if ($dh = @opendir($dir)) {
          while (($file = readdir ($dh)) != false) {
             if (($file == ".") || ($file == "..")) continue;
                if (is_dir($dir . '/' . $file))
                   delete_directory($dir . '/' . $file);
                else
                   unlink($dir . '/' . $file);
          }
          @closedir($dh);
          rmdir($dir);
      }
   }
$dir = "./fakeDir";
delete_directory($dir);
?>

           
       








Related examples in the same category

1.Function Recursion Demo
2.A recursive function is a function that calls itself from within its own code.
3.Using a recursive function to sum an integer set
4.Recursive Function Example
5.Recursive Functions