Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts

    Getting subfolders within another folder

    I'm trying to get every single sub-folder (and only folders, not files), that is contained within another folder.


    Right now the following is outputting: stillsstillstills
    It should be outputting: story_board
    PHP Code:
    function ListFolders($path){
    $dir_handle=opendir($path) or die("Unable to open $path");
    $dirname end(explode("/"$path));
    $dirnames=array();
        while(
    false!==($file=readdir($dir_handle))){
            if (
    $entry!="." && $entry!="..") {
            
    $dirnames[]=$dirname;
            }
        }
    closedir($dir_handle); 
    return 
    $dirnames;

    PHP Code:
    $folders=ListFolders("./media/stills");
    foreach(
    $folders as $folder){
    $content2.=$folder;

    Last edited by HDRebel88; 12-18-2012 at 12:40 AM.

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    PHP Code:
    function ListFolders($base){
    $dir_array=array();
    if (!
    is_dir($base)){
        return 
    $dir_array;
    }
    if(
    $dh=opendir($base)){
        while ((
    $file=readdir($dh))!==false){
            if (
    $file!= '.' || $file!= '..'){
                if (
    is_dir($base.'/'.$file)) {
                
    $dir_array[] = $file;
                }
                else{
                
    array_merge($dir_arrayrendertask::ListFolders($base.'/'.$file));
                }
            }
        }
        
    closedir($dh);
        
    $dir_array=array_slice($dir_array,2);
        return 
    $dir_array;
    }

    Had to use array_slice because it prepended three dots to the array. Which was a single dot array element, then a double dot array element.


  •  

    Posting Permissions

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