note 106222 added to function.scandir
| From: | Anonymous at osu1 dot php dot net | Date: | Wed, 19 Oct 2011 17:58:13 +0000 |
| Subject: | note 106222 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184003@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 190.222.188.132
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/106222
Del: integrated -- https://master.php.net/note/delete/106222/integrated
Del: useless -- https://master.php.net/note/delete/106222/useless
Del: bad code -- https://master.php.net/note/delete/106222/bad+code
Del: spam -- https://master.php.net/note/delete/106222/spam
Del: non-english -- https://master.php.net/note/delete/106222/non-english
Del: in docs -- https://master.php.net/note/delete/106222/in+docs
Del: other reasons-- https://master.php.net/note/delete/106222
Reject -- https://master.php.net/note/reject/106222
Search -- https://master.php.net/manage/user-notes.php