Viewed   90 times

I have this working function that finds folders and creates an array.

function dua_get_files($path)
    foreach (glob($path . "/*", GLOB_ONLYDIR) as $filename)
        $dir_paths[] = $filename;   
    return $dir_paths;

This function can only find the directories on the current location. I want to find the directory paths in the child folders and their children and so on.

The array should still be a flat list of directory paths.

An example of how the output array should look like

$dir_path[0] = 'path/folder1';
$dir_path[1] = 'path/folder1/child_folder1';
$dir_path[2] = 'path/folder1/child_folder2';
$dir_path[3] = 'path/folder2';
$dir_path[4] = 'path/folder2/child_folder1';
$dir_path[5] = 'path/folder2/child_folder2';
$dir_path[6] = 'path/folder2/child_folder3';



If you want to recursively work on directories, you should take a look at the RecursiveDirectoryIterator.

$path = realpath('/etc');

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
    echo "$namen";
Thursday, October 13, 2022

Try this:

glob("$dir/{,[1-9]}[0-9].jpg", GLOB_BRACE);

The GLOB_BRACE option tells it to recognize alternatives in braces, so {,[1-9]} matches either an empty string or a non-zero digit. Then this is followed by any digit. So with the empty string it matches 0 through 9, and with the non-zero digit it matches 10 through 99.

Sunday, November 13, 2022
if(glob("images/*.{jpg,png,gif}", GLOB_BRACE))
  //create gallery

And that's about it :)

Saturday, October 15, 2022

use a flag for that. hope to help.(-:

$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
foreach ($images as $image){
echo '<div class="item' .($flag?' active':''). '">'.PHP_EOL."tt";
echo '<img src="'.$image.'" alt=""></div>'.PHP_EOL."t";
Saturday, August 20, 2022

This seems to be covered as an issue in this bug report on

The last post on that thread is about it not being a bug, but an issue from how glob treats brackets, as part of the regular expression. I'm not sure I agree. It seems you can work around this, unless you cannot move up into the parent folder.

If you remove the first requirement of being in the inside [test] folder, you can get the file listing by using a syntax like below:

$glob = glob("[[]test[]]/*");

Given these complications, I would recommend not using the glob function if you are running into issues on windows machines, and look at other file listing functions like readdir.

Saturday, December 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :