PHP Get Directories and Only Directories

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Nice simple way to get a list of directories and only directories from the current directory while excluding dot and dot-dot.

PHP Code: [ Select ]
<?php
 
$dirs = glob('./*', GLOB_ONLYDIR);
 
print_r($dirs);
 
?>
  1. <?php
  2.  
  3. $dirs = glob('./*', GLOB_ONLYDIR);
  4.  
  5. print_r($dirs);
  6.  
  7. ?>


For a directory listing that looks like the following

BASH Code: [ Select ]
joebert@home:~/Desktop/test$ ls -la
total 20
drwxrwxrwx  4 joebert joebert 4096 2010-01-09 13:55 .
drwxr-xr-x 18 joebert joebert 4096 2010-01-09 13:47 ..
drwxr-xr-x  2 joebert joebert 4096 2010-01-09 13:55 dir-one
drwxr-xr-x  2 joebert joebert 4096 2010-01-09 13:55 dir-two
-rw-r--r--  1 joebert joebert    0 2010-01-09 13:55 one.txt
-rw-r--r--  1 joebert joebert  130 2009-12-21 01:03 _.php
  1. joebert@home:~/Desktop/test$ ls -la
  2. total 20
  3. drwxrwxrwx  4 joebert joebert 4096 2010-01-09 13:55 .
  4. drwxr-xr-x 18 joebert joebert 4096 2010-01-09 13:47 ..
  5. drwxr-xr-x  2 joebert joebert 4096 2010-01-09 13:55 dir-one
  6. drwxr-xr-x  2 joebert joebert 4096 2010-01-09 13:55 dir-two
  7. -rw-r--r--  1 joebert joebert    0 2010-01-09 13:55 one.txt
  8. -rw-r--r--  1 joebert joebert  130 2009-12-21 01:03 _.php


This method will return the following

Code: [ Select ]
Array
(
  [0] => ./dir-one
  [1] => ./dir-two
)
  1. Array
  2. (
  3.   [0] => ./dir-one
  4.   [1] => ./dir-two
  5. )
  • jpitassi
  • Born
  • Born
  • jpitassi
  • Posts: 1

Post 3+ Months Ago

Thanks for this. I knew there was a way to get it using one line. much appreciated.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 71 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.