Results 1 to 4 of 4
  1. #1
    Not a Moderator Malcolm's Avatar
    Join Date
    Nov 2001
    Location
    Ontario, Canada
    Posts
    1,589

    PHP directory recursion weirdness

    I wrote a function that recursively grabs a directory tree.

    Here my directory structure:
    Code:
    /
    /directory
    /directory/sub-dir
    /directory/sub-dir/sub-sub-dir
    /new1
    /new2
    /new2/new3
    /new2/new4
    /new2/new5
    /new2/new5/new6
    This is my code:
    PHP Code:
    /*function: list_tree()
    VARS:
    $dir = start path
    $level = how deep the dir is in the tree (recursive entry only)
    */
    function list_tree($dir$level=0)
    {
        
    // Define Arrays
        
    $rec = array();
        
    $dirs = array();
        
        if (
    is_dir($dir)){
            if (
    $dh opendir($dir)) {
                while ((
    $file readdir($dh)) !== false) {
                    if(!
    preg_match("#\.#"$file) && is_dir($dir "/" $file) && !is_file($dir "/" $file) && !is_link($dir "/" $file)){
                        
    $dirs[] = $dir "/" $file."&".$level;
                        
    $rec list_tree($dir "/" $file$level+1);
                    }
    //end:if
                    
    for($x 0$x sizeof($rec); $x++){
                        
    $dirs[] = $rec[$x];
                    }
    //end:for
                
    }//end:while
                
    closedir($dh);
            }
    //end:if
        
    }//end:if
        
    return $dirs;
    }
    //end:functions 
    With this code I should see
    Code:
    directory&1
    sub-dir&2
    sub-sub-dir&3
    new1&1
    new2&1
    new3&2
    new4&4
    new5&4
    new6&5
    But what i get is
    Code:
    directory&1
    and
    Code:
    sub-dir&2
    sub-sub-dir&3
    sub-sub-dir&3
    sub-sub-dir&3
    repeated 7 times, then the other right stuff.

    Any idea whats going on?

    To view a live example jump to http://www.thenwing.com/testing

    Quitters never Win, Winners never Quit; but those who never Win and never Quit are generally banned EmuTalk members.


    • Advertising

      advertising
      EmuTalk.net
      has no influence
      on the ads that
      are displayed
        
       

  2. #2
    Not a Moderator Malcolm's Avatar
    Join Date
    Nov 2001
    Location
    Ontario, Canada
    Posts
    1,589
    Get an answer, just had to replace my for loop with a array_merge.
    Incase anyone wants the full code for their own use:
    Quote Originally Posted by http://www.phpbuilder.com/board/showthread.php?s=&postid=10560394#post10 560369
    PHP Code:
    function list_tree($dir$level=0

        
    // Define Arrays
        
    $rec = array(); 
        
    $dirs = array(); 

        if (
    is_dir($dir)){ 
            if (
    $dh opendir($dir)) { 
                while ((
    $file readdir($dh)) !== false) { 
                    if (
    is_dir($dir '/' $file
                         && !
    is_link($dir '/' $file)
                         && (
    $file != '.'
                         && (
    $file != '..')) {
                        
    $dirs[] = $dir "/" $file."&".$level
                        
    $rec list_tree($dir "/" $file$level+1); 
                        
    $dirs array_merge($dirs$rec);
                    }
    //end:if
                
    }//end:while
                
    closedir($dh); 
            }
    //end:if
        
    }//end:if
        
    return $dirs
    }
    //end:functions 
    Quitters never Win, Winners never Quit; but those who never Win and never Quit are generally banned EmuTalk members.

  3. #3
    I Am The Stig Martin's Avatar
    Join Date
    Nov 2001
    Location
    Malmö
    Posts
    3,814
    Oh yeah, was just about to give you the solutions.


    The Emulation64 Network offers you free hosting!
    Click here for requirements and contact info.

  4. #4
    Not a Moderator Malcolm's Avatar
    Join Date
    Nov 2001
    Location
    Ontario, Canada
    Posts
    1,589
    Quote Originally Posted by Martin
    Oh yeah, was just about to give you the solutions.
    Thanks anyways Marty-pants
    Quitters never Win, Winners never Quit; but those who never Win and never Quit are generally banned EmuTalk members.

Similar Threads

  1. Help with VB6 pls
    By Olger901 in forum TechTalk
    Replies: 9
    Last Post: February 14th, 2006, 10:49
  2. Exchange Provisioning & PHP
    By LoneRaven in forum Website Corner
    Replies: 0
    Last Post: January 1st, 2004, 03:53
  3. Recursion vs. Iteration
    By Slougi in forum Programming
    Replies: 7
    Last Post: May 7th, 2003, 21:36
  4. PHP coding help
    By Vchat20 in forum TechTalk
    Replies: 6
    Last Post: January 30th, 2003, 03:53
  5. Replies: 4
    Last Post: May 14th, 2002, 05:20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •