Hướng dẫn php list directories only

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?

asked Jun 27, 2011 at 19:23

Jeff ThomasJeff Thomas

4,69011 gold badges37 silver badges57 bronze badges

0

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

answered Feb 5, 2013 at 13:08

0

Check out the PHP docs for readdir[]. It includes an example for exactly this.

For completeness:


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.

answered Jun 27, 2011 at 19:25

Jason McCrearyJason McCreary

69.8k21 gold badges128 silver badges170 bronze badges

4

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.

answered Jun 27, 2011 at 19:27

phihagphihag

268k67 gold badges441 silver badges463 bronze badges

2

List only folders [Directories]:


answered Mar 28, 2013 at 10:10

T.ToduaT.Todua

50.1k19 gold badges216 silver badges213 bronze badges

1

Try this with glob['*'] function

    

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.

answered Nov 25, 2015 at 9:41

Chaminda BandaraChaminda Bandara

1,9352 gold badges27 silver badges30 bronze badges

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 :-]


    General Image
    
    

answered Nov 30, 2015 at 22:19

Chủ Đề