从目录-PHP递归获取父级

时间:2018-02-20 08:07:08

标签: php arrays recursion directory

我使用PHP扫描目录,我可以获得每个级别的深度,但是如何将每个文件夹与其父级匹配!

function getDirContents2($dir, &$results = array(), $depth=1 , $parent=0){
$files = scandir($dir);

foreach($files as $key => $value){
    $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
    if(is_dir($path)) {
        if($value != "." && $value != "..") {
            $parent= $parent+1;
            getDirContents2($path, $results, $depth+1 , $parent);
            $parent= $parent-1;
            $results[] = $depth . '-' . $parent. '-' .  $path ;

        }

    }  
}

return $results;

输出:

array (size=5)
  0 => string '3-2-C:\wamp64\www\qa\drive\1\F1-LEVEL1\F1-LEVEL2\F1-LEVEL3' (length=58)
  1 => string '2-1-C:\wamp64\www\qa\drive\1\F1-LEVEL1\F1-LEVEL2' (length=48)
  2 => string '2-1-C:\wamp64\www\qa\drive\1\F1-LEVEL1\F2-LEVEL2' (length=48)
  3 => string '1-0-C:\wamp64\www\qa\drive\1\F1-LEVEL1' (length=38)
  4 => string '1-0-C:\wamp64\www\qa\drive\1\F2-LEVEL1' (length=38)

1 个答案:

答案 0 :(得分:0)

我使用 DirectoryIterator 来获取具有文件夹级别的当前密钥。

class Seek_Iterator{

    private $is_recursive;

    public function get_seekable_files_obj($path){

        return new DirectoryIterator($path);
    }

    public function process_iterator($path, $offset = false, $is_recursive = false){

        $iterator = $this->get_seekable_files_obj($path);

        if (empty($iterator)) {
            return ;
        }

        $this->seek = empty($offset) ? array() : explode('-', $offset);

        $this->counter = 0;
        $this->is_recursive = $is_recursive;

        if ($is_recursive) {
            $this->recursive_iterator($iterator, false);
        } else {
            $this->iterator($iterator);
        }
    }


    public function iterator($iterator){
        //Moving satelite into position.
        $this->seek_offset($iterator);

        while ($iterator->valid()) {

            $this->counter++;

            $recursive_path = $iterator->getPathname();

            //Dont recursive iterator if its a dir or dot
            if ($iterator->isDot() || !$iterator->isReadable()  || $iterator->isDir()) {

                //move to next file
                $iterator->next();

                continue;
            }

            $key = $iterator->key();

            echo $recursive_path . ' - ' .$key . "<br>";

            //move to next file
            $iterator->next();
        }

    }


    public function recursive_iterator($iterator, $key_recursive) {

        $this->seek_offset($iterator);

        while ($iterator->valid()) {

            //Forming current path from iterator
            $recursive_path = $iterator->getPathname();

            //Mapping keys
            $key = ($key_recursive !== false ) ? $key_recursive . '-' . $iterator->key() : $iterator->key() ;

            echo $recursive_path . ' - ' .$key . "<br>";

            //Do recursive iterator if its a dir
            if (!$iterator->isDot() && $iterator->isReadable() && $iterator->isDir() ) {

                //create new object for new dir
                $sub_iterator = new DirectoryIterator($recursive_path);

                $this->recursive_iterator($sub_iterator, $key);

            }

            //move to next file
            $iterator->next();
        }

    }

    private function seek_offset(&$iterator){

        if(!count($this->seek)){
            return false;
        }

        //Moving satelite into position.
        $iterator->seek($this->seek[0]);

        //remove positions from the array after moved satelite
        unset($this->seek[0]);

        //reset array index
        $this->seek = array_values($this->seek);

    }
}

用法:

$obj = new Seek_Iterator();
$obj->process_iterator('/var/www/html/test/', $offset = false , $recursive = true);

如果您愿意,可以指定$offset

示例输出:

/var/www/html/test/backups/2016/03 - 0
/var/www/html/test/backups/2016/03/. - 0-0
/var/www/html/test/backups/2016/03/.. - 0-1
/var/www/html/test/backups/2016/03/favicon.png - 0-2
/var/www/html/test/backups/2016/03/fullscreen-slide-1.jpg - 0-3
/var/www/html/test/backups/2016/03/fullscreen-slide-1-125x112.jpg - 0-4
/var/www/html/test/backups/2016/. - 1
/var/www/html/test/backups/2016/.. - 2
/var/www/html/test/backups/2016/10 - 3
/var/www/html/test/backups/2016/10/. - 3-0
/var/www/html/test/backups/2016/10/.. - 3-1
/var/www/html/test/backups/2016/10/centro-duna-01-300x189.jpg - 3-2
/var/www/html/test/backups/2016/10/centro-duna-01.jpg - 3-3
/var/www/html/test/backups/2016/10/centro-duna-01-125x112.jpg - 3-4
/var/www/html/test/backups/2016/10/centro-duna-01-150x150.jpg - 3-5
/var/www/html/test/backups/2016/09 - 4
/var/www/html/test/backups/2016/09/. - 4-0
/var/www/html/test/backups/2016/09/cropped-logo-home-100x40.png - 4-1
/var/www/html/test/backups/2016/09/.. - 4-2
/var/www/html/test/backups/2016/09/cropped-logo-home.png - 4-3
/var/www/html/test/backups/2016/09/cropped-logo-home-125x40.png - 4-4