how to delete folder ??

Discussion in 'PHP' started by myharshdesigner, Oct 11, 2007.

  1. #1
    I want to delete folder i use this code :-

    rmdir($folder);
    Code (markup):
    but if folder contain some file or some folders then i will get this error :-


    Now i ant to delete all files and folder along with the main folder so how to do that ?
     
    myharshdesigner, Oct 11, 2007 IP
  2. kkrizka

    kkrizka Peon

    Messages:
    223
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The comments on the PHP documentation page are very useful to read, they contains answers to many common questions. :)

    From http://ca.php.net/manual/en/function.rmdir.php :

    
    <?php
     function rmdir_r($path) {
       if (!is_dir($path)) {return false;}
       $stack = Array($path);
       while ($dir = array_pop($stack)) {
         if (@rmdir($dir)) {continue;}
         $stack[] = $dir;
         $dh = opendir($dir);
         while (($child = readdir($dh)) !== false) {
           if ($child[0] == '.') {continue;}
           $child = $dir . DIRECTORY_SEPARATOR . $child;
           if (is_dir($child)) {$stack[] = $child;}
           else {unlink($child);}
         }
       }
       return true;
     }
     ?>
    
    Code (markup):
     
    kkrizka, Oct 11, 2007 IP