Re: List files in a directory using filters and exclusions
| From: | Clancy | Date: | Sun, 07 Jun 2009 22:50:08 +0000 |
| Subject: | Re: List files in a directory using filters and exclusions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-293717@lists.php.net to get a copy of this message | ||
On Sat, 6 Jun 2009 21:39:15 -0400 (CDT), rsantamaria@hab.uci.cu ("Rolando Santamaria
Maso") wrote:
>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();
I have just written a recursive backup program, and used a slightly different exclusion
procedure:
$exclusions = array ('...drupal' => true, 'Images' =>'true',
.... );
if (!isset($exclusions[$file])) { [copy file to backup ] }
This looks simpler, and therefore quicker, but I suspect it is just putting the complexity
out of sight!
>
>------------------------------------------------------------
>Greetings!!!!
>