如果您只需要一个简单的数组,递归地包含所有文件(而不是目录),请尝试
<?php
function get_file_list_recursively(string $dir, bool $realpath = false): array
{
 $files = array();
 $files = [];
 foreach ((new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS))) as $file) {
 if ($realpath) {
 $files[] = $file->getRealPath();
 } else {
 $files[] = $file->getPathname();
 }
 }
 return $files;
}
?>
示例返回值
<?php
array (
 0 => '/home/hans/scraping/1650518081RESULTS.txt',
 1 => '/home/hans/scraping/1650518121RESULTS.txt',
 2 => '/home/hans/scraping/1650518679RESULTS.txt',
 3 => '/home/hans/scraping/1650518780RESULTS.txt',
 4 => '/home/hans/scraping/1650522198RESULTS.txt',
 5 => '/home/hans/scraping/1650522927RESULTS.txt',
 6 => '/home/hans/scraping/1650525391RESULTS.txt',
 7 => '/home/hans/scraping/check_cache.php',
 8 => '/home/hans/scraping/foo/bar.txt',
 9 => '/home/hans/scraping/foobar.txt',
 10 => '/home/hans/scraping/GoodProxyDaemon.php',
);
?>
(给编辑的备注,如果您能找到更好的示例返回值,请随时覆盖上面的示例)