What potential issues can arise when using a recursive function to scan directories in PHP?

One potential issue when using a recursive function to scan directories in PHP is the possibility of running into infinite loops if the function is not properly structured. To avoid this, you should include a base case that checks for when the function should stop recursing, such as when there are no more subdirectories to scan.

function scanDirectory($dir) {
    $files = scandir($dir);
    
    foreach($files as $file) {
        if ($file != '.' && $file != '..') {
            $path = $dir . '/' . $file;
            
            if (is_dir($path)) {
                scanDirectory($path);
            } else {
                echo $path . "\n";
            }
        }
    }
}

// Usage
scanDirectory('/path/to/directory');