/    Sign up×
Community /Pin to ProfileBookmark

list only directories

I have a script that I am using to list all directories and files.

I am trying to seperate out the files and folders all I want is the names of the folders.

Does anyone know of a way to do this?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJan 27.2010 — [code=php]printf("<pre>&#37;s</pre>", print_r(glob("/path/to/parent/dir/*", GLOB_ONLYDIR), true));[/code]
Copy linkTweet thisAlerts:
@dk_zero-coolJan 27.2010 — Just use the is_dir and is_file to check whether it is the one or the other.

[code=php]if($objDir = opendir(".")) {
while(($item = readdir($objDir)) !== false) {
if(is_dir($item) && $item != "." && $item != "..") {
echo $item." is a directory";

}elseif(is_file($item)) {
echo $item." is a file";
}
}
closedir($objDir);
}[/code]
Copy linkTweet thisAlerts:
@MindzaiJan 28.2010 — Seems overly complicated for something that can be done with a single function call...
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 28.2010 — Seems overly complicated for something that can be done with a single function call...[/quote]

I agree. It seems glob() has been around since 4.3.0 and is a killer function, but many people continue to use the other methods. I know when I first saw the command in a function listing by name only, I assumed it tied into a wacky database type i.e. blob's and clob's! Funky name until I realized it's like a libc function. It really does save effort using it, plus any time a language construct or function can do it why not use it?

I always appreciate more efficient code.
Copy linkTweet thisAlerts:
@MindzaiJan 28.2010 — It's been around as a unix program for a long time as well, so I'm surprised more people aren't aware of it, but as you say most code examples you see use the readdir approach. It's quite rare that I ever use readdir, glob is just so much cleaner.
Copy linkTweet thisAlerts:
@dk_zero-coolJan 28.2010 — Well. I would think that the reason people use the readdir instead of glob, is the same reason that people don't use preg_split and preg_replace if it can be done with explode and str_replace.

In my case however, I just didn't know that one ?
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 28.2010 — Don't feel bad, we all didn't at one time! :p
Copy linkTweet thisAlerts:
@NogDogJan 28.2010 — Just a tip: if you only want the file names (no path info), you can change to the directory before doing the glob().
[code=php]
$cwd = getcwd();
if(chdir('path/to/files'))
{
$dirs = glob(*, GLOB_ONLYDIR);
chdir($cwd);
}
[/code]

While it's no longer a one-liner, it still avoids having to do any looping in the script.
×

Success!

Help @Xtrme_XJ 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...