Skip to content

Instantly share code, notes, and snippets.

@cdeocampo
Created October 22, 2012 06:48
Show Gist options
  • Select an option

  • Save cdeocampo/3930015 to your computer and use it in GitHub Desktop.

Select an option

Save cdeocampo/3930015 to your computer and use it in GitHub Desktop.
PHP: Get files to array
<?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