SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    The MacGyver of Design bronze trophy Johan Dahlström's Avatar
    Join Date
    Oct 2004
    Location
    Uppsala, Sweden
    Posts
    517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Listing sub directories with SPL

    I have been playing around a little bit with SPL the latest days to solve a specific problem. I want to list sub directories 1 step below to an dir with SPL, sound simple enough I thought. But I was wrong and now I'm just getting dizzy from all the iterator examples and hierarchical structures that I can no longer seperate right from left. Peeked a little bit at http://cvs.php.net/pecl/spl/, but that just got me more confused. So a helping hand is very much appreciated.

    Here's the principle

    Code:
    Directories
    
    * Dir1
        |- Subdir 1 - Subdir2
        
    * Dir2
       |- No sub directories
       
    * Dir3
       |- Subdir1
    I guess you get the ide.

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Örebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Below is a simple example of how to accomplish this.

    PHP Code:
    <?php

    $rootFolder 
    = new RecursiveDirectoryIterator('/path/to/folder');
    echo 
    '<pre style="font-size: 12px">';

    while( 
    $rootFolder->valid() )
    {
      if( 
    $rootFolder->isDir() && !$rootFolder->isDot() )
      {
        echo 
    '* '$rootFolder->getFilename(), "\n";
        
    $subFolder $rootFolder->getChildren();
        
        while( 
    $subFolder->valid() )
        {
          if( 
    $subFolder->isDir() && !$subFolder->isDot() )
          {
            echo 
    '  - '$subFolder->getFilename(), "\n";
          }
          
          
    $subFolder->next();
        }
      }
      
      
    $rootFolder->next();
    }

    ?>
    Yours, Erik.

  3. #3
    The MacGyver of Design bronze trophy Johan Dahlström's Avatar
    Join Date
    Oct 2004
    Location
    Uppsala, Sweden
    Posts
    517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "But, but..."

    I tried that, but evidently not in the right way. Thanks.

  4. #4
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what you are looking for has been posted as an example on the PHP.NET site at

    http://php.net/spl/

    Look for the post by "mastabog at hotmail dot com"

  5. #5
    The MacGyver of Design bronze trophy Johan Dahlström's Avatar
    Join Date
    Oct 2004
    Location
    Uppsala, Sweden
    Posts
    517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wonderful. Often misses the manual when it comes to PHP 5 due to its, let's say pretty much non-existent documentation.


Bookmarks

Posting Permissions

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