List files in a directory using filters and exclusions
| From: | Rolando Santamaria Maso | Date: | Sun, 07 Jun 2009 01:39:15 +0000 |
| Subject: | List files in a directory using filters and exclusions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-293687@lists.php.net to get a copy of this message | ||
Hi PHP community, here I bring a very simple and interesting function that
I made for a project, it's objetive is "to list path files in a directory
with the possibility of using filters and exclusions".
What do you think about?
-----------------------
function get_files($path, &$result, Array $exclusions, Array $filters)
{
@$dir_content = scandir($path);
if ($dir_content)
{
$end = count($dir_content);
$i = 2;
for ($i; $i < $end; $i++)
{
$path_and_element = $path.'/'.$dir_content[$i];
if (array_search($path_and_element, $exclusions) === false)
{
if (array_search(substr($dir_content[$i],
strlen($dir_content[$i]) - 4), $filters) !== false)
{
$result[] = $path_and_element;
}
if (is_dir($path_and_element))
{
get_files($path_and_element, $result, $exclusions,
$filters);
}
}
}
}
}
$path = '/var/www';
$result = array();
$exclusions = array('/var/www/drupal', '/var/www/wp');
$filters = array('.php');
get_files($path, $result, $exclusions, $filters);
echo "<pre>";
print_r($result);
die();
------------------------------------------------------------
Greetings!!!!