note 106222 deleted from function.scandir by danbrown
| From: | danbrown@php.net | Date: | Thu, 20 Oct 2011 17:08:28 +0000 |
| Subject: | note 106222 deleted from function.scandir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184030@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
This is a simple loop that will help you get all subdirectories inside one specific folder. The
result is an array of arrays containing the paths of alls subdirectories ordered by subdirectory
depth within the root folder.
<?php
$allDirs = array();
$dirLevel = 0;
$keepLooking = TRUE;
$searchFactor = 0;
$searchPath = '';
while ($keepLooking) {
if($dirLevel == 0){
$searchPath = '/initial/path/here';//Path for the root directory
$tmp = array();
foreach (scandir($searchPath) as $filename) {
$host = $searchPath.'/'.$filename;
if (is_dir($host) && substr($filename,0,1)!='.'){
$searchFactor++;
array_push($tmp,$host);
}
}
if($searchFactor>0){
$dirLevel++;
array_push($allDirs, $tmp);
$searchFactor = 0;
}
}
else{
$tmp = array();
foreach ($allDirs[$dirLevel-1] as $searchPath) {
foreach (scandir($searchPath) as $filename) {
$host = $searchPath.'/'.$filename;
if (is_dir($host) && substr($filename,0,1)!='.'){
$searchFactor++;
array_push($tmp,$host);
}
}
}
if($searchFactor>0){
$dirLevel++;
array_push($allDirs, $tmp);
$searchFactor = 0;
}
else
$keepLooking = FALSE;
}
}
//To see the result
var_dump($allDirs);
?>