编写函数时遇到问题,返回一个目录中包含的目录,然后将所有每个目录文件路径放入一个名为$ files的数组中。
我的目录集合工作正常,但似乎无法创建文件数组
这是我到目前为止的代码:
<?php
function find_recursive_images_and_dirs($path, $option) {
try {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $path) {
if ($path->isDir()) {
if(substr($path->__toString(), (strrpos($path->__toString(), "/")+1)) != "." && substr($path->__toString(), (strrpos($path->__toString(), "/")+1)) != ".."){
$directories[] = $path->__toString();
$childrenIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(($path->__toString() . "/")), RecursiveIteratorIterator::CHILD_FIRST);
//echo $path->getChildren() . "</br>";
echo "<pre>" . var_dump($path) . "</pre>";
foreach ($childrenIterator as $child) {
echo "Entered!2";
if(substr($path->__toString(), (strrpos($path->__toString(), "/")+1)) != "." && substr($path->__toString(), (strrpos($path->__toString(), "/")+1)) != ".."){
echo "Entered!3";
echo $child->__toString();
$files[] = $child->__toString();
}
}
}
} /*else {
$files[] = $path->__toString();
}*/
}
if($option == "files")
return $files;
else if($option == "directories")
return $directories;
} catch (Exception $e) {
echo "/* There was a problem, error: " . $e . " */";
}
}
$path = "../administrator/panel/gallery/public/";
$files = find_recursive_images_and_dirs($path, "files");
$directories = find_recursive_images_and_dirs($path, "directories");
print_r($files);
?>
我尝试了getChildren()方法,但它只是吐出一个错误,所以我最终尝试使用在检查文件是否是主文件夹中的目录时返回的dir路径创建一个新的递归目录迭代器。然后程序使用该路径并执行相同的操作,只是这次它将每个文件夹中列出的文件放在名为$ files的数组中,但由于某种原因,数组中没有存储任何内容,事实上,它没有&#39 ;甚至进入第二个foreach()。