-1

Any idea how to get my files using only file extension? Eg. (.cfg)

I tried foreach (glob("*.cfg") as $filename) but i need to know the path in order to use glob method.

1 Answers1

0

As mentioned by @bansi, you should

try RecursiveDirectoryIterator

The example given there should work for you:

$basepath        = "root/directory/for/search";
$start_directory = new RecursiveDirectoryIterator($basepath);
$iterator        = new RecursiveIteratorIterator($iterator);
$result          = new RegexIterator($iterator, '/^.+\.cfg$/i', RecursiveRegexIterator::GET_MATCH);

After you got your results, you can access your file paths in a list just as you entered it via $basepath:

foreach( iterator_to_array($result) as $file)
{
    echo "config found at: $file[0]";
}

You might find this post useful as well.

A little more background can be found in this article.

Community
  • 1
  • 1
T3 H40
  • 2,326
  • 8
  • 32
  • 43