How can I delelte a tree directory structure with php4

Discussion in 'PHP' started by +:::Spider25:::+, Nov 16, 2006.

  1. #1
    How can I delelte a tree directory structure with php4?
    I can have any number of directories with any number of files inside.


    Thanks a lot for the answers
     
    +:::Spider25:::+, Nov 16, 2006 IP
  2. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You would use the unlink() command:

    http://www.php.net/manual/en/function.unlink.php

    You would need to build a loop structure to enter each directory and delete the files within it.

    I believe there is a sample function there in the annotations that will do it. I did not check it for accuracy.
     
    ajscottsr, Nov 16, 2006 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    
    
    function empty_directory($dir_name){
          $directory_content = glob ("$dir_name/*",  GLOB_BRACE);
          foreach($directory_content as $item_name){
                if(is_file($item_name)){ unlink($item_name);}
                if(is_dir($item_name)){ empty_directory($item_name);}
          }
          return;
    }
    
    //Empty deleteme directory
    empty_directory('deleteme');
    
    
    PHP:
    This should do.

    Peace,
     
    Barti1987, Nov 16, 2006 IP
  4. +:::Spider25:::+

    +:::Spider25:::+ Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a lot
     
    +:::Spider25:::+, Dec 1, 2006 IP
  5. crazyden

    crazyden Guest

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you want to delete a tree, and a tree - is an array, then you would need to unset every item of an array in cycle, like:

    
    foreach ( $array as $i => $item )
      unset ($array[$i]);
    
    PHP:
     
    crazyden, Dec 1, 2006 IP