Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get all folders in a given folder using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 208
    Comment on it

    Hello Reader's! If you want to get all of the folder inside a given directory then PHP offers you many ways to do that, Lets see them from the basic programs.

    you can use glob() with GLOB_ONLYDIR option here's how you can see how to retrieve only directories with GLOB:

    $directories = glob($somePath . '/*' , GLOB_ONLYDIR);
    

    Or if you want to make a recursive function then you can use the code as follows:-

    <?php
        /*this will do what you asked for, it only returns the subdirectory names in a given
          path, and you can make hyperlinks and use them:
        */
    
        $yourStartingPath = "photos\\";
        $iterator = new RecursiveIteratorIterator( 
            new RecursiveDirectoryIterator($yourStartingPath),  
            RecursiveIteratorIterator::SELF_FIRST);
    
        foreach($iterator as $file) { 
            if($file->isDir()) { 
                $path = strtoupper($file->getRealpath()) ; 
                $path2 = PHP_EOL;
                $path3 = $path.$path2;
    
                $result = end(explode('/', $path3)); 
    
                echo "<br />". basename($result );
            } 
        } 
    
    
    ?>
    

    Now the output of the code will be the folder names inside the directory.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: