PHP List Directory structure and exclude some directories

前端 未结 8 708
南笙
南笙 2020-12-18 05:44

I have this PHP Code:

$rootpath = \'../admin/\';
$inner = new RecursiveDirectoryIterator($rootpath);
$fileinfos = new RecursiveIteratorIterator($inner);

for         


        
8条回答
  •  無奈伤痛
    2020-12-18 06:17

    In essence, my answer is not much different from Thomas' answer. However, he does not get a few things correct:

    • The semantics correct for the RecursiveCallbackFilterIterator require you to return true to recurse into subdirectories.
    • He doesn't skip the . and .. directories inside each sub-directory
    • His in_array check doesn't quite do what he expects

    So, I wrote this answer instead. This will work correctly, assuming I understand what you want:

    Edit: He has since fixed 2 of those three issues; the third may not be an issue because of the way he wrote his conditional check but I am not quite sure.

    hasChildren() && !in_array($file->getFilename(), $exclude)) {
            return true;
        }
        return $file->isFile();
    };
    
    $innerIterator = new RecursiveDirectoryIterator(
        $directory,
        RecursiveDirectoryIterator::SKIP_DOTS
    );
    $iterator = new RecursiveIteratorIterator(
        new RecursiveCallbackFilterIterator($innerIterator, $filter)
    );
    
    foreach ($iterator as $pathname => $fileInfo) {
        // do your insertion here
    }
    

提交回复
热议问题