0

我正在编写我想递归列出目录的代码。到目前为止,我已经编写了列出 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>';
4

1 回答 1

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('');
于 2012-08-09T18:07:53.193 回答