/    Sign up×
Community /Pin to ProfileBookmark

Is there a simple glob to get files in sub dirs?

I don’t have any issue with glob or how it works but I was wondering, since I am not completely PHP literate, if there is some way to glob search all child dirs and check if that child dir has a child dir and so forth?

i am using nested foreach loops to read sub dirs right now but is there a one command way to do this?

the prob with the way i am doing it is if i add another child dir, i have to manually add another nested foreach to search for it otherwise any files i put in it will not be read by the current code since it uses as many nested for’s as i knwo there are sub dirs.

?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 07.2006 — Don't know if this qualifies as "simple", but it seems to work:
[code=php]
function recurse_dir($dir, &$array)
{
if(!is_dir($dir))
{
trigger_error("recurse_dir: first param must be a directory", E_USER_NOTICE);
return(FALSE);
}
$array = glob("$dir/*");
if(is_array($array))
{
foreach($array as $value)
{
if(is_dir($value))
{
recurse_dir($value, $array[$value]);
}
}
}
}
// TEST IT:
recurse_dir($_SERVER['DOCUMENT_ROOT'], $test);
echo "<pre>";
print_r($test);
echo "</pre>";
[/code]
Copy linkTweet thisAlerts:
@rch10007authorApr 07.2006 — That would certainly be an efficeint way of doing it!

keep rotating through the function until teh value is no longer a directory, awesome - never could get that line of thought process in my head, lol!

using your code with a directory consisting of 24 folders and 469 files, makes teh printed array look pretty, now i have to figure out a way of sort the array into chunks i can use.

what i'm trying to do is make like a very simple and as small as possible flat file based CMS for my teacher that shows the sub directories of a root dir and the files and sub directories that are inside of those. is it possible to do this with under 100 lines of code, that is the max i can use!
×

Success!

Help @rch10007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...