note 106225 deleted from function.is-dir by danbrown
| From: | danbrown@php.net | Date: | Thu, 20 Oct 2011 17:07:36 +0000 |
| Subject: | note 106225 deleted from function.is-dir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184027@lists.php.net to get a copy of this message | ||
Note Submitter: alcala_ruben2 at hotmail dot com
----
This is a simple loop that will help you get all subdirectories (except hidden ones) 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);
?>
I hope it helps someone.
-Ruben-