src/File.php
public static function directoryToArray(
        string $directory,
        bool $recursive = false,
        bool $listDirs = false,
        bool $listFiles = true,
        string $exclude = '',
        array $allowedFileTypes = [],
        bool $relativePath = false
    ) : array {
        $arrayItems = [];
        $skipByExclude = false;
        $handle = opendir($directory);
        if ($handle) {
            while (false !== ($file = readdir($handle))) {
                preg_match(
                    '/(^(([\.]) {1,2})$|(\.(svn|git|md|htaccess))|(Thumbs\.db|\.DS_STORE|\.|\.\.))$/iu',
                    $file,
                    $skip
                );
                if ($exclude) {
                    preg_match($exclude, $file, $skipByExclude);
                }
                if ($allowedFileTypes && !is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
                    $ext = strtolower(preg_replace('/^.*\.([^.]+)$/D', '$1', $file));
                    if (!in_array($ext, $allowedFileTypes)) {
                        $skip = true;
                    }
                }
                if (!$skip && !$skipByExclude) {
                    if (is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
                        if ($recursive) {
                            $arrayItems = array_merge(
                                $arrayItems,
                                self::directoryToArray(
                                    $directory . DIRECTORY_SEPARATOR . $file,
                                    $recursive,
                                    $listDirs,
                                    $listFiles,
                                    $exclude,
                                    $allowedFileTypes,
                                    $relativePath
                                )
                            );
                        }
                        if ($listDirs) {
                            $arrayItems[] = (
                            $relativePath
                                ? $file
                                : $directory . DIRECTORY_SEPARATOR . $file
                            );
                        }
                    } else {
                        if ($listFiles) {
                            $arrayItems[] = (
                            $relativePath
                                ? $file
                                : $directory . DIRECTORY_SEPARATOR . $file
                            );
                        }
                    }
                }
            }
            closedir($handle);
        }
        return $arrayItems;
    }
public static function directoryToArray(
        string $directory,
        bool $recursive = false,
        bool $listDirs = false,
        bool $listFiles = true,
        string $exclude = '',
        array $allowedFileTypes = [],
        bool $relativePath = false
    ) : array {
        $arrayItems = [];
        $skipByExclude = false;
        $handle = opendir($directory);
        if ($handle) {
            while (false !== ($file = readdir($handle))) {
                preg_match(
                    '/(^(([\.]) {1,2})$|(\.(svn|git|md|htaccess))|(Thumbs\.db|\.DS_STORE|\.|\.\.))$/iu',
                    $file,
                    $skip
                );
                if ($exclude) {
                    preg_match($exclude, $file, $skipByExclude);
                }
                if ($allowedFileTypes && !is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
                    $ext = strtolower(preg_replace('/^.*\.([^.]+)$/D', '$1', $file));
                    if (!in_array($ext, $allowedFileTypes)) {
                        $skip = true;
                    }
                }
                if (!$skip && !$skipByExclude) {
                    if (is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
                        if ($recursive) {
                            $arrayItems = array_merge(
                                $arrayItems,
                                self::directoryToArray(
                                    $directory . DIRECTORY_SEPARATOR . $file,
                                    $recursive,
                                    $listDirs,
                                    $listFiles,
                                    $exclude,
                                    $allowedFileTypes,
                                    $relativePath
                                )
                            );
                        }
                        if ($listDirs) {
                            $arrayItems[] = (
                            $relativePath
                                ? $file
                                : $directory . DIRECTORY_SEPARATOR . $file
                            );
                        }
                    } else {
                        if ($listFiles) {
                            $arrayItems[] = (
                            $relativePath
                                ? $file
                                : $directory . DIRECTORY_SEPARATOR . $file
                            );
                        }
                    }
                }
            }
            closedir($handle);
        }
        return $arrayItems;
    }
public static function directoryToArray ( string $ directory , bool $ recursive = false , bool $ listDirs = false , bool $ listFiles = true , string $ exclude = '' , array $ allowedFileTypes = [ ] , bool $ relativePath = false ) : array { $ arrayItems = [ ] ; $ skipByExclude = false ; $ handle = opendir ( $ directory ) ; if ( $ handle ) { while ( false !== ( $ file = readdir ( $ handle ) ) ) { preg_match ( '/(^(([\.]) {1,2})$|(\.(svn|git|md|htaccess))|(Thumbs\.db|\.DS_STORE|\.|\.\.))$/iu' , $ file , $ skip ) ; if ( $ exclude ) { preg_match ( $ exclude , $ file , $ skipByExclude ) ; } if ( $ allowedFileTypes && ! is_dir ( $ directory . DIRECTORY_SEPARATOR . $ file ) ) { $ ext = strtolower ( preg_replace ( '/^.*\.([^.]+)$/D' , '$1' , $ file ) ) ; if ( ! in_array ( $ ext , $ allowedFileTypes ) ) { $ skip = true ; } } if ( ! $ skip && ! $ skipByExclude ) { if ( is_dir ( $ directory . DIRECTORY_SEPARATOR . $ file ) ) { if ( $ recursive ) { $ arrayItems = array_merge ( $ arrayItems , self :: directoryToArray ( $ directory . DIRECTORY_SEPARATOR . $ file , $ recursive , $ listDirs , $ listFiles , $ exclude , $ allowedFileTypes , $ relativePath ) ) ; } if ( $ listDirs ) { $ arrayItems [ ] = ( $ relativePath ? $ file : $ directory . DIRECTORY_SEPARATOR . $ file ) ; } } else { if ( $ listFiles ) { $ arrayItems [ ] = ( $ relativePath ? $ file : $ directory . DIRECTORY_SEPARATOR . $ file ) ; } } } } closedir ( $ handle ) ; } return $ arrayItems ; }
Get an array that represents directory tree
@param string  $directory     Directory path
@param boolean $recursive     *[Optional]* Include sub directories
@param boolean $listDirs      *[Optional]* Include directories on listing
@param boolean $listFiles     *[Optional]* Include files on listing
@param string  $exclude       *[Optional]* Exclude paths that matches this
regular expression
@param string[]   $allowedFileTypes *[Optional]* Allowed file extensions,
default `[]` (allow all)
@param boolean $relativePath *[Optional]* Return paths in relative form,
default `false`
@link https://github.com/phramework/phramework/blob/master/src/Models/Util.php Source
@return array
Get an array that represents directory tree