Created
May 5, 2015 03:43
-
-
Save GoldenEra/e6f0062864b429c05e5b to your computer and use it in GitHub Desktop.
php:read files
This file contains 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
/** | |
* A simple function that find all files by extension an return it by an array. | |
* | |
* @since [1.0] | |
* @see [http://php.net/manual/en/function.glob.php] | |
* @date 2015-05-05 | |
* | |
* @param [string] $directory | |
* @param array $extensions like "xml,zip,txt" | |
* @return [array] array("zip" => array( [0] => "D:\a.zip", [1] => "D:\b.zip"), "xml" => array( [0] => "D:\1.xml")) | |
*/ | |
function findFiles($directory, $extensions = array()) { | |
function glob_recursive($directory, &$directories = array()) { | |
foreach(glob($directory, GLOB_ONLYDIR | GLOB_NOSORT) as $folder) { | |
$directories[] = $folder; | |
glob_recursive("{$folder}/*", $directories); | |
} | |
} | |
glob_recursive($directory, $directories); | |
$files = array (); | |
foreach($directories as $directory) { | |
foreach($extensions as $extension) { | |
foreach(glob("{$directory}/*.{$extension}") as $file) { | |
$files[$extension][] = $file; | |
} | |
} | |
} | |
return $files; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment