PHP - Listing directories recursively and finding files

Asked

Viewed 288 times

1

Good morning everyone, I would like a help from the staff, I need to deliver a project on PHP and I find myself stuck in a certain part of it, I need to recursively list all the directories that are inside the "D:\".

Example:

D:\diretorio01\backup001\

D:\diretorio02\backup002\
              \backup003\

D:\diretorio03\backup004\

And after listing the directories, I need to locate within them all the files .Bak.

It is possible to create this structure?

Thanks in advance for the support of all involved.

1 answer

0

Yes, it is possible, just use the function glob:

foreach (glob('D:/**/*.bak') as $path) {
    echo $path, PHP_EOL;
}

It will fetch all files that have the extension .bak in any directory within D:/.

  • Says Anderson, thank you very much for answering, in parts it worked yes, but he did not locate . Bak that were inside the D: and did not recursively locate inside directories above the D:**. Being a little clearer, no matter how many directories I have inside D: or inside subdirectories, I need to locate them all. Bak from D: to how many existing directory levels in D: .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.