What are the best practices for creating a recursive function in PHP to search through directories and files?

When creating a recursive function in PHP to search through directories and files, it is important to define a base case that stops the recursion, iterate through the directories and files using a loop, and call the recursive function within itself for each subdirectory found.

function searchFiles($dir){
    $files = scandir($dir);
    
    foreach($files as $file){
        if ($file == '.' || $file == '..'){
            continue;
        }
        
        $fullPath = $dir . '/' . $file;
        
        if(is_dir($fullPath)){
            searchFiles($fullPath);
        } else {
            echo $fullPath . PHP_EOL;
        }
    }
}

// Call the function with the starting directory
searchFiles('/path/to/directory');