PHP Glob function to find Controller files

204 Views Asked by At

I try to find all the controller files of a java code repository in php script.(Lets say CustomerController.java for example)

Here is the solution i have tried to achieve this goal:

$fileScan = glob($currentDirectory . "**/*Controller.java");

But it returns nothing. I have also tried different combinations like:

"**Controller*.java", "*/*Controller*.java" etc.

But not luck.

Am i missing something here about glob function?

2

There are 2 best solutions below

2
On

Use RecursiveDirectoryIterator

<?php
function rsearch($folder, $pattern) {
    $dir = new RecursiveDirectoryIterator($folder);
    $ite = new RecursiveIteratorIterator($dir);
    $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
    $fileList = array();
    foreach($files as $file) {
        $fileList = array_merge($fileList, $file);
    }
    return $fileList;
}
?>
1
On

Try following code. It will find the files with "Controller.java"

foreach (glob("*Controller.java") as $filename) 
{
    echo $filename;
}