PHP get file listing including sub directories

Here's a simpler approach:

Instead of using:

$path = realpath('yourfolder/examplefolder/*');
glob($path);

You'll have to use:

$path = realpath('yourfolder/examplefolder').'/{**/*,*}';
glob($path, GLOB_BRACE);

This last one will use bracing, and it is, in fact, a shorthand for this code:

$path = realpath('yourfolder/examplefolder');

$self_files = glob($path . '/*');
$recursive_files = glob($path . '/**/*');

$all_files = $self_files + $recursive_files; // That's the result you want

You may also want to filter directories from your result. glob() function has GLOB_ONLYDIR flag. Let's use it to diff out our result.

$path =  realpath('yourfolder/examplefolder/') . '{**/*,*}';

$all_files = array_diff(
  glob($path, GLOB_BRACE),
  glob($path, GLOB_BRACE | GLOB_ONLYDIR)
);

from glob example

if ( ! function_exists('glob_recursive'))
{
    // Does not support flag GLOB_BRACE        
   function glob_recursive($pattern, $flags = 0)
   {
     $files = glob($pattern, $flags);
     foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
     {
       $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
     }
     return $files;
   }
}

Solution:

<?php
$path = realpath('yourfolder/examplefolder');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
        echo "$filename</br>";
}
?>

This function supports GLOB_BRACE:

function rglob($pattern_in, $flags = 0) {
    $patterns = array ();
    if ($flags & GLOB_BRACE) {
        $matches;
        if (preg_match_all ( '#\{[^.\}]*\}#i', $pattern_in, $matches )) {
            // Get all GLOB_BRACE entries.
            $brace_entries = array ();
            foreach ( $matches [0] as $index => $match ) {
                $brace_entries [$index] = explode ( ',', substr ( $match, 1, - 1 ) );
            }

            // Create cartesian product.
            // @source: https://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays
            $cart = array (
                    array () 
            );
            foreach ( $brace_entries as $key => $values ) {
                $append = array ();
                foreach ( $cart as $product ) {
                    foreach ( $values as $item ) {
                        $product [$key] = $item;
                        $append [] = $product;
                    }
                }
                $cart = $append;
            }

            // Create multiple glob patterns based on the cartesian product.
            foreach ( $cart as $vals ) {
                $c_pattern = $pattern_in;
                foreach ( $vals as $index => $val ) {
                    $c_pattern = preg_replace ( '/' . $matches [0] [$index] . '/', $val, $c_pattern, 1 );
                }
                $patterns [] = $c_pattern;
            }
        } else
            $patterns [] = $pattern_in;
    } else
        $patterns [] = $pattern_in;

    // @source: http://php.net/manual/en/function.glob.php#106595
    $result = array ();
    foreach ( $patterns as $pattern ) {
        $files = glob ( $pattern, $flags );
        foreach ( glob ( dirname ( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) {
            $files = array_merge ( $files, rglob ( $dir . '/' . basename ( $pattern ), $flags ) );
        }
        $result = array_merge ( $result, $files );
    }
    return $result;
}

Tags:

Php

Glob