public static function listStructure($dir = ".", $filters = array(), $recurse = true, $sort = "asc", $strip_root = null, $exclude = array(), $ignore_hidden = true)
    {
        if (!is_dir($dir)) {
            return null;
        }

        // Check to see if $filters is a string containing a regex, or an array of file types
        if (is_array($filters) && !empty($filters)) {
            $filter_type = 'file';
        } elseif (is_string($filters)) {
            $filter_type = 'regex';
        } else {
            $filter_type = null;
        }
        $files = array();

        $prefix = str_replace($strip_root, '', $dir);

        if ($prefix !== "" && substr($prefix, -1) !== "/") {
            $prefix .= "/";
        }

        $files['dirlist'] = array();
        $files['filelist'] = array();

        foreach (scandir($dir) as $file) {
            if (
                ($file == '.' || $file === '..')
                || ($ignore_hidden && $file{0} === '.')
                || in_array($file, $exclude)
                || in_array("$dir/$file", $exclude)
            ) {
                continue;
            }

            $dir = rtrim($dir, '/');

            if (is_dir("$dir/$file")) {
                if ($recurse) {
                    $files["$prefix$file/"] = self::listStructure("$dir/$file", $filters, $recurse, $sort, $strip_root, $exclude, $ignore_hidden);
                }

                $files['dirlist'][] = "$prefix$file/";
            } elseif ($filter_type === 'regex') {
                if (preg_match($filters, $file)) {
                    $files['filelist'][] = "$prefix$file";
                }
            } elseif ($filter_type === 'file') {
                if (in_array(self::getExtension($file), $filters)) {
                    $files['filelist'][] = "$prefix$file";
                }
            } elseif (is_null($filter_type)) {
                $files['filelist'][] = "$prefix$file";
            }
        }

        if (is_array($files['filelist'])) {
            ($sort == 'desc') ? rsort($files['filelist']) : sort($files['filelist']);
        }

        return $files;
    }
public static function listStructure($dir = ".", $filters = array(), $recurse = true, $sort = "asc", $strip_root = null, $exclude = array(), $ignore_hidden = true)
    {
        if (!is_dir($dir)) {
            return null;
        }

        // Check to see if $filters is a string containing a regex, or an array of file types
        if (is_array($filters) && !empty($filters)) {
            $filter_type = 'file';
        } elseif (is_string($filters)) {
            $filter_type = 'regex';
        } else {
            $filter_type = null;
        }
        $files = array();

        $prefix = str_replace($strip_root, '', $dir);

        if ($prefix !== "" && substr($prefix, -1) !== "/") {
            $prefix .= "/";
        }

        $files['dirlist'] = array();
        $files['filelist'] = array();

        foreach (scandir($dir) as $file) {
            if (
                ($file == '.' || $file === '..')
                || ($ignore_hidden && $file{0} === '.')
                || in_array($file, $exclude)
                || in_array("$dir/$file", $exclude)
            ) {
                continue;
            }

            $dir = rtrim($dir, '/');

            if (is_dir("$dir/$file")) {
                if ($recurse) {
                    $files["$prefix$file/"] = self::listStructure("$dir/$file", $filters, $recurse, $sort, $strip_root, $exclude, $ignore_hidden);
                }

                $files['dirlist'][] = "$prefix$file/";
            } elseif ($filter_type === 'regex') {
                if (preg_match($filters, $file)) {
                    $files['filelist'][] = "$prefix$file";
                }
            } elseif ($filter_type === 'file') {
                if (in_array(self::getExtension($file), $filters)) {
                    $files['filelist'][] = "$prefix$file";
                }
            } elseif (is_null($filter_type)) {
                $files['filelist'][] = "$prefix$file";
            }
        }

        if (is_array($files['filelist'])) {
            ($sort == 'desc') ? rsort($files['filelist']) : sort($files['filelist']);
        }

        return $files;
    }
public static function listStructure ( $ dir = "." , $ filters = array ( ) , $ recurse = true , $ sort = "asc" , $ strip_root = null , $ exclude = array ( ) , $ ignore_hidden = true ) { if ( ! is_dir ( $ dir ) ) { return null ; } // Check to see if $filters is a string containing a regex, or an array of file types if ( is_array ( $ filters ) && ! empty ( $ filters ) ) { $ filter_type = 'file' ; } elseif ( is_string ( $ filters ) ) { $ filter_type = 'regex' ; } else { $ filter_type = null ; } $ files = array ( ) ; $ prefix = str_replace ( $ strip_root , '' , $ dir ) ; if ( $ prefix !== "" && substr ( $ prefix , - 1 ) !== "/" ) { $ prefix .= "/" ; } $ files [ 'dirlist' ] = array ( ) ; $ files [ 'filelist' ] = array ( ) ; foreach ( scandir ( $ dir ) as $ file ) { if ( ( $ file == '.' || $ file === '..' ) || ( $ ignore_hidden && $ file { 0 } === '.' ) || in_array ( $ file , $ exclude ) || in_array ( "$dir/$file" , $ exclude ) ) { continue ; } $ dir = rtrim ( $ dir , '/' ) ; if ( is_dir ( "$dir/$file" ) ) { if ( $ recurse ) { $ files [ "$prefix$file/" ] = self :: listStructure ( "$dir/$file" , $ filters , $ recurse , $ sort , $ strip_root , $ exclude , $ ignore_hidden ) ; } $ files [ 'dirlist' ] [ ] = "$prefix$file/" ; } elseif ( $ filter_type === 'regex' ) { if ( preg_match ( $ filters , $ file ) ) { $ files [ 'filelist' ] [ ] = "$prefix$file" ; } } elseif ( $ filter_type === 'file' ) { if ( in_array ( self :: getExtension ( $ file ) , $ filters ) ) { $ files [ 'filelist' ] [ ] = "$prefix$file" ; } } elseif ( is_null ( $ filter_type ) ) { $ files [ 'filelist' ] [ ] = "$prefix$file" ; } } if ( is_array ( $ files [ 'filelist' ] ) ) { ( $ sort == 'desc' ) ? rsort ( $ files [ 'filelist' ] ) : sort ( $ files [ 'filelist' ] ) ; } return $ files ; }
Construct a multi-dimensional array that reflects the directory
structure of a given path grouped into directory and file keys
matching any input constraints.

@param string $dir (optional)
the path of the directory to construct the multi-dimensional array
for. this defaults to '.'.
@param array|string $filters (optional)
either a regular expression to filter the files by or an array of
files to include.
@param boolean $recurse (optional)
true if sub-directories should be traversed and reflected in the
resulting array, false otherwise.
@param string $sort (optional)
'asc' if the resulting filelist array should be sorted, anything else otherwise.
this defaults to 'asc'.
@param mixed $strip_root (optional)
If null, the full path to the file will be returned, otherwise the value
of `strip_root` will be removed from the file path.
@param array $exclude (optional)
ignore files listed in this array. this defaults to an empty array.
@param boolean $ignore_hidden (optional)
ignore hidden files (i.e. files that begin with a period). this defaults
to true.
@return null|array
return the array structure reflecting the input directory or null if
the input directory is not actually a directory.
Construct a multi - dimensional array that reflects the directory structure of a given path grouped into directory and file keys matching any input constraints .