note 106225 added to function.is-dir
| From: | alcala_ruben2 at hotmail dot com | Date: | Wed, 19 Oct 2011 19:50:54 +0000 |
| Subject: | note 106225 added to function.is-dir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184006@lists.php.net to get a copy of this message | ||
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-
----
Server IP: 69.147.83.197
Probable Submitter: 190.222.188.132
----
Manual Page -- http://www.php.net/manual/en/function.is-dir.php
Edit -- https://master.php.net/note/edit/106225
Del: integrated -- https://master.php.net/note/delete/106225/integrated
Del: useless -- https://master.php.net/note/delete/106225/useless
Del: bad code -- https://master.php.net/note/delete/106225/bad+code
Del: spam -- https://master.php.net/note/delete/106225/spam
Del: non-english -- https://master.php.net/note/delete/106225/non-english
Del: in docs -- https://master.php.net/note/delete/106225/in+docs
Del: other reasons-- https://master.php.net/note/delete/106225
Reject -- https://master.php.net/note/reject/106225
Search -- https://master.php.net/manage/user-notes.php