我正在编写代码,我想以递归方式列出目录。到目前为止,我已经编写了列出1级目录的代码。有没有人对我如何能无限地进行推理有什么想法?
$res = OC_Files::getDirectoryContent('');
$list = array();
foreach($res as $file) {
if ($file['type'] == 'dir') {
$res1 = OC_Files::getDirectoryContent('/'.$file['name']);
foreach($res1 as $res2) {
if ($res2['type'] == 'file') $list[] = $file['name'].'/'.$res2['name'];
}
} else $list[] = $file['name'];
}
foreach($list as $entry)
echo $entry.'</br>';
答案 0 :(得分:0)
试试这个:
function traverse_directory($dir) {
$res = OC_Files::getDirectoryContent($dir);
$list = array();
foreach($res as $file) {
if ($file['type'] == 'dir') {
traverse_directory($dir . '/' . $file['name'])
} else {
$list[] = $file['name'];
}
}
foreach($list as $entry) {
echo $entry.'</br>';
}
}
traverse_directory('');