PHP opendir() to list folders only
Asked Answered
M

6

18

I would like to use opendir() to list only the folders in a particular folder (i.e. /www/site/). I would like to exclude files from the list as well at the '.' and '..' folders that appear on a linux folder listing. How would I go about doing this?

Mccarley answered 27/6, 2011 at 19:23 Comment(0)
I
22

Check out the PHP docs for readdir(). It includes an example for exactly this.

For completeness:

<?php
if ($handle = opendir('.')) {
    $blacklist = array('.', '..', 'somedir', 'somefile.php');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
            echo "$file\n";
        }
    }
    closedir($handle);
}
?>

Simply change opendir('.') to your directory, i.e. opendir('/www/sites/'), and update $blacklist to include the names of files or directory you do not wish to output.

Infection answered 27/6, 2011 at 19:25 Comment(4)
This only filters . and .., not other directories.Caddy
Thank you this worked... I was looking at the man for opendir I did not see the page for readdir.Mccarley
@Price, no worries. Glad it helped. @downvoter, care to weigh in?Infection
@Prince: Often on the manual page there are links to related functions. Often worth the click for completeness.Odom
C
22
foreach(glob('directory/*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace('directory/', '', $dir);
    echo $dir;
}

You can use simply glob with GLOB_ONLYDIR and then filter resulted directories

Configurationism answered 5/2, 2013 at 13:8 Comment(0)
C
8
function scandir_nofolders($d) {
   return array_filter(scandir($d), function ($f) use($d) {
       return ! is_dir($d . DIRECTORY_SEPARATOR . $f);
   });
}

This function returns an array you can iterate over or store somewhere, which is what 99.37% of all programmers using opendir want.

Caddy answered 27/6, 2011 at 19:27 Comment(2)
The filters are failing because, in the anonymous function, $dir is not in scope, and so show up empty. Not sure how to resolve this..Anecdotist
@Anecdotist Oops, you're right, this is totally missing use($d). Fixed.Caddy
D
7

List only folders (Directories):

<?php
$Mydir = ''; ### OR MAKE IT 'yourdirectory/';

foreach(glob($Mydir.'*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace($Mydir, '', $dir);
    echo $dir;
}
?>
Dravidian answered 28/3, 2013 at 10:10 Comment(1)
You could also use echo basename($dir); instead of str_replace.Cislunar
L
3

Try this with glob('*') function

    <?php
    $dirs = array_filter(glob('*'), 'is_dir');
    $i = 1;
    foreach ($dirs as $value) {
        echo $i . '. &nbsp; <a href = "http://localhost/' . $value . '" target = "_blank">' . $value . '</a><br>';
        $i++;
    }
    ?>

Above code worked for me for list out folders in current directory and I further developed code to open each folder in a new tab in same browser. This is shows only directories.

Leopold answered 25/11, 2015 at 9:41 Comment(0)
A
2

Can also be used in forms to create a Dropdown of Folder names (here it is the images folder). Ensures that a user uploading an image pushes it to the correct folder :-)

<select name="imgfolder">
    <option value="genimage">General Image</option>
    <?php
    $Mydir = '../images/'; //  use 'anydirectory_of_your_choice/';

    foreach(glob($Mydir.'*', GLOB_ONLYDIR) as $dir) {
    $dirname =  basename($dir) ;
    echo '<option value="' . $dirname . '">' . $dirname . '</option>'  ; 
    }
    ?>
    </select>
Awry answered 30/11, 2015 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.