我正在使用此功能从文件夹及其子文件夹中获取所有文件。这很好。但是我需要所有这些文件的数组。
function get_all_directory_and_files($dir){
$dh = new DirectoryIterator($dir);
// Dirctary object
foreach ($dh as $item) {
if (!$item->isDot()) {
if ($item->isDir()) {
get_all_directory_and_files("$dir/$item");
} else {
echo $dir . "/" . $item->getFilename();
echo "<br>";
}
}
}
}
# Call function
get_all_directory_and_files("data/comments");
所以我怎么能把所有文件放到这样的数组中:
$all_files[] = // contains all the files
<?php
function get_all_directory_and_files($dir) {
$results = [];
$dh = new DirectoryIterator($dir);
// Dirctary object
foreach ($dh as $item) {
if (!$item->isDot()) {
if ($item->isDir()) {
$results = array_merge($results, get_all_directory_and_files($dir . DIRECTORY_SEPARATOR . $item));
} else {
$results[] = $dir . DIRECTORY_SEPARATOR . $item->getFilename();
}
}
}
return $results;
}
var_dump( get_all_directory_and_files("/tmp") );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句