Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luanrodriguesp's avatar

Filesystem - list directories

Hello guys, i'm trying to list all folders from a directories, i done it, but not the way i want, when i say to list (Storage::directories('/mainfolder'), i get a list of sub-folders like...


mainfolder\. <----- this i dont want to list
mainfolder\.. <----- this i dont want to list
mainfolder\subfolder1 <--- this is ok
mainfolder\subfolder2 <--- this is ok
mainfolder\subfolder3 <--- this is ok

anyone know to do not list that folders above???

0 likes
16 replies
bobbybouwmann's avatar

You can also just leave out those directories with an if statement for example, but that's just the easy way ;)

bobbybouwmann's avatar

I would do something like this:

$dir = 'app';

function listAllFolders($dir) {
    $ffs = scandir($dir);   
    
    $json = array();
    $newjson = array();
    
    foreach ($ffs as $ff) {
        if ($ff != '.' && $ff != '..')  {
            if (is_readable($dir . '/' . $ff))  {
                if (is_dir($dir . '/' . $ff))  {
                    $json[] = $dir . '/' . $ff;
                    $newjson = listAllFolders($dir . '/' . $ff);
                }

                $json = array_merge($json, $newjson);
            }
        }
    }

    return $json;
}
1 like
deadpix3l's avatar

How about the PHP DirectoryIterator class? It has an isDot() method...

$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if (!$fileinfo->isDot()) {
        echo $fileinfo->getFilename() . "\n";
    }
}
1 like
joedawson's avatar
Level 18

If I remember rightly, Storage::directories will return an array.

If, the . & .. directories are always first - you could simply unset them.

$directories = Storage::disk('local')->directories('mainfolder');
unset($directories[0], $directories[1]);

dd($directories);
2 likes
michaeldyrynda's avatar

I would be recommending what @deadpix3l suggested. I'd advise against mucking around with parsing strings or assuming that the dot files will always be returned first. Use the functionality the language provides wherever possible, they've done the hard work so you don't have to.

1 like
deadpix3l's avatar

I wouldn't like to assume that dot files are returned exactly the same in every call either. The DirectoryIterator class is part of the Standard PHP Library and is provided to allow all kinds of functionality that you could want from iterating through a directory structure. It's definitely the cleanest way to do what you were originally asking.

1 like
michaeldyrynda's avatar

I think some people may misunderstand the purpose of the Storage service.

@mathiasgrimm perhaps the OP doesn't want to expose their filesystem or provide some way to allow end users to traverse up the file system from the base directory. Safety first ;)

mathiasgrimm's avatar

I was just wondering the initial purpose, maybe there is another way to solve the problem

bobbybouwmann's avatar

That's not the correct answer.. How do you know for sure that the folder has those directories?

joedawson's avatar

If the dot's aren't always returned or aren't always first - then you could also just do this then.

$directories = Storage::disk('local')->directories('mainfolder');
$directories = array_diff($directories, ['.', '..']);

dd($directories);

No need to over complicate it.

2 likes
michaeldyrynda's avatar

@blackbird on a Linux-based system, the . (current) and .. (parent) directories are always present.

Given that the OP specifically asked about them, I made the reasonable assumption that he was a) working on a Linux-based system and b) would deploy to one.

@JoeDawson that solution would work, also; it's certainly simpler and much more reliable than assuming that the dotfiles will be returned first.

1 like
bobbybouwmann's avatar

Oke fair enough, but if something goes wrong I will be the first to say "I told you so"! Hehe :D

Mort's avatar

I would definitely reccomend the solution by @JowDawson, much simpler and more reliable.

Please or to participate in this conversation.