note 101136 deleted from function.opendir by danbrown
| From: | danbrown@php.net | Date: | Mon, 29 Nov 2010 15:55:55 +0000 |
| Subject: | note 101136 deleted from function.opendir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174440@lists.php.net to get a copy of this message | ||
Note Submitter: frank dot s dot demarco at gmail dot com
----
Here's an extension of the previously posted rec_listFiles function. It recursively lists the
contents of a directory. You can optionally specify an extension to search for. You can also
specify a directory or directories to exclude.
The excludes parameter can be a string or array of strings. This function has not been tested
extensively.
<?php
function get_directory_contents($directory_name, $extension="", $excludes=NULL)
{
$excludes = (!is_array($excludes)) ? array($excludes) : $excludes;
$directory = opendir($directory_name);
$expression = "/^[^#.].*" . $extension . "$/";
$files = array();
while ($entry = readdir($directory))
{
if ($entry == "." || $entry == "..") continue;
$path = $directory_name . "/" . $entry;
if (preg_match($expression, $entry))
{
$files[] = $path;
}
if (is_dir($path))
{
if (!in_array($path, $excludes))
{
$files = array_merge(
$files, get_directory_contents($path, $extension, $excludes));
}
}
}
return $files;
}
?>
"style" Directory Contents:
".", "..", "style.css", "toolbar/",
"toolbar/toolbar.css", "toolbar/themes/",
"toolbar/themes/grayscale.css"
get_directory_contents("style")
Array ( [0] => style/toolbar [1] => style/toolbar/themes [2] =>
style/toolbar/themes/grayscale.css [3] => style/toolbar/toolbar.css [4] => style/style.css )
get_directory_contents("style", ".css")
Array ( [0] => style/toolbar/themes/grayscale.css [1] => style/toolbar/toolbar.css [2] =>
style/style.css )
get_directory_contents("style", ".css", "style/toolbar/themes")
Array ( [0] => style/toolbar/toolbar.css [1] => style/style.css )