Created
October 22, 2012 06:48
-
-
Save cdeocampo/3930015 to your computer and use it in GitHub Desktop.
PHP: Get files to array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function getFiles($path, $extensions=null, &$files=null) | |
| { | |
| if(!is_dir($path)) return null; | |
| $files = ($files==null)?array():$files; | |
| $extensions = is_array($extensions) ? implode('|', $extensions) : $extensions; | |
| if ($handle = opendir($path)) { | |
| while (false !== ($entry = readdir($handle))) { | |
| if(preg_match("/^.*\.(". $extensions .")$/i", $entry)) { | |
| $files[] = $entry; | |
| } else if(is_dir($path.'/'.$entry) && !($entry == '.' || $entry == '..')) { | |
| $this->getFiles($path.'/'.$entry, $extensions, $files); | |
| } | |
| } | |
| closedir($handle); | |
| } | |
| return $files; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment