note 101136 added to function.opendir
| From: | frank dot s dot demarco at gmail dot com | Date: | Mon, 29 Nov 2010 05:07:38 +0000 |
| Subject: | note 101136 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174427@lists.php.net to get a copy of this message | ||
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 )
----
Server IP: 69.147.83.197
Probable Submitter: 121.185.48.194
----
Manual Page -- http://www.php.net/manual/en/function.opendir.php
Edit -- https://master.php.net/note/edit/101136
Del: integrated -- https://master.php.net/note/delete/101136/integrated
Del: useless -- https://master.php.net/note/delete/101136/useless
Del: bad code -- https://master.php.net/note/delete/101136/bad+code
Del: spam -- https://master.php.net/note/delete/101136/spam
Del: non-english -- https://master.php.net/note/delete/101136/non-english
Del: in docs -- https://master.php.net/note/delete/101136/in+docs
Del: other reasons-- https://master.php.net/note/delete/101136
Reject -- https://master.php.net/note/reject/101136
Search -- https://master.php.net/manage/user-notes.php