如果你只需要一个简单的递归数组,包含所有文件(而不是目录),可以尝试:
<?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',
);
?>
(注意编辑,如果你能找到更好的示例返回值,请随意覆盖上面的示例)