1
目前我正在通過php的目錄。我可以列出任何給定目錄的子目錄。但是,結果並不是我正在尋找的100%。下面的代碼返回子目錄,但另外它還返回數組中的主目錄。我怎樣才能返回目錄的子目錄?RecursiveIteratorIterator獲取子目錄
$root = '/test';
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
$paths = array($root);
foreach ($iter as $path => $dir) {
if ($dir->isDir()) {
$paths[] = $path;
}
}
電流輸出
Array ([0] => /test [1] => /test/asf8 [2] => /test/some2 [3] => /test/something)
所需的輸出
Array ([0] => /test/asf8 [1] => /test/some2 [2] => /test/something)
如果你不想返回'/ test'目錄,然後不要將它添加到陣列中。只要移除'$ paths = array($ root);'行,或者將其改爲'$ paths = array();' – Jeemusu