Jump to content

Looking through directories for cetain subdirectories


danscreations

Recommended Posts

Really not sure how to do this and can't seem to find the correct resources to find out. So if there is a simple answer that be great if not a point in the correct direction would be much appreciated.

 

Basicly need to find folder XYZ in several directories which have subdirectores. If the folder XYZ is contained in the directory or sub directories I'm going to use that path.

scandir

 

opendir

 

Without any attempt at code you probably will not get much help.

 

The following was taken from a comment on the scandir page, I am sure you can tweak it to your needs.

 

<?php
/**
         * returns the folder content names
         *
         * @param string $rootDir path to the root folder
         * @param array $allowext allowed extensions
         * @param array $notallownames not allowed names
         * @param array $allData array by reference which collects the root content
         */
        static function ScanDirectories($rootDir, $allowext, $notallownames, &$allData)
        {
              $dirContent = scandir($rootDir);
              static $i=0;
            foreach($dirContent as $key => $content)
            {
                if ($content == '.' || $content == '..') continue;
                $path = $rootDir . SEPARATOR . $content;
                $ext = substr($content, strrpos($content, '.') + 1);
                if(in_array($ext, $allowext) && is_file($path) && is_readable($path) && !in_array($content, $notallownames))
                {
                    $allData[$i][name] = $content;
                    $allData[$i][path] = $rootDir;
                    $allData[$i][sub] = '';
                }
                else if(is_dir($path) && is_readable($path) && !in_array($content, $notallownames))
                {
                    $allData[$i][name] = $content;
                    $allData[$i][path] = $rootDir;
                    $allData[$i][sub] = array();
                  
                    self::scanDirectories($path, $allowext, $notallownames, &$allData[$i][sub]);
                }
                $i++;
            }
        }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.