PHP List Directory structure and exclude some directories

前端 未结 8 678
南笙
南笙 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:14

    My suggestion is to try using Symfony's finder library as it makes alot of this much easier and is easily installed via composer here are the docs

    http://symfony.com/doc/current/components/finder.html

    and here is a simple example of something similar to what i think you're asking

    
     * @since 12/6/13, 12:52 PM
     * @link http://www.clarkt.com
     * @copyright Clark Tomlinson © 2013
     *
     */
    
    require_once('vendor/autoload.php');
    
    $finder = new Finder();
    $directories = $finder->directories()
                          ->in(__DIR__)
                          ->ignoreDotFiles(true)
                          ->exclude(array('one', 'two', 'three', 'four'))
                          ->depth(0);
    
    foreach ($directories as $dir) {
        echo '
    ';
        print_r($dir->getRealPath());
        echo '
    '; }

    That example will return all directories without transversing into them to change that change or remove the depth.

    To get all files in that directory do something similar to this

    
     * @since 12/6/13, 12:52 PM
     * @link http://www.clarkt.com
     * @copyright Clark Tomlinson © 2013
     *
     */
    
    require_once('vendor/autoload.php');
    
    $finder = new Finder();
    $directories = $finder->directories()
                          ->in(__DIR__)
                          ->ignoreDotFiles(true)
                          ->exclude(array('one', 'two', 'three', 'four'))
                          ->depth(0);
    
    foreach ($directories as $dir) {
        $files = $finder->files()
                        ->ignoreDotFiles(true)
                        ->in($dir->getRealPath());
    
        foreach ($files as $file) {
            echo '
    ';
            print_r($file);
            echo '
    '; } }

提交回复
热议问题