简体   繁体   中英

Recursively adding all sub directories and files from a directory into an array

I have a function that will obtain all directories and files from a given directory, however the way it obtains them makes it a little more complicated to work with:

function getFileLists($dir, $recursive=FALSE) {
    // retrieve an array of all the directories and files in a certain directory
    $retval = [];
    if (substr($dir, -1) !== "/") {
        $dir .= "/";
    }
    $d = @dir($dir) or die("unable to open {$dir} for reading, permissions?");
    while(FALSE !== ($entry = $d->read())) {
        if ($entry{0} === ".") { continue; }
        if (is_dir("{$dir}{$entry}")) {
            $retval[] = [
                'name' => "{$dir}{$entry}",
                'last_modified' => filemtime("{$dir}{$entry}")
            ];
            if($recursive && is_readable("{$dir}{$entry}/")) {
                $retval = array_merge($retval, getFileLists("{$dir}{$entry}/", TRUE));
            }
        } elseif (is_readable("{$dir}{$entry}")) {
            $retval[] = [
                'name' => "{$dir}{$entry}",
                'last_modified' => filemtime("{$dir}{$entry}")
            ];
        }
    }
    $d->close();
    return $retval;
}

When you run this on a given directory it will produce the following results:

array(14) {
  [0]=>
  array(2) {
    ["name"]=>
    string(15) "./kb_data/admin"
    ["last_modified"]=>
    int(1543591247)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(28) "./kb_data/admin/testfile.txt"
    ["last_modified"]=>
    int(1543591238)
  }
  ...
}

Which is great but this makes it pretty difficult to work with seeing as how I want to display this directory as a tree . What I'm actually wanting to do is the produce something along the lines of the following:

array(14) {
  [0]=>
    array(3) {
    ["name"]=>
    string(16) "./kb_data/shared"
    ["last_modified"]=>
    int(1543591258)
    ["files"] => array(#) {
        ["name"]=>
        string(29) "./kb_data/shared/testfile.txt"
        ["last_modified"]=>
        int(1543591238)
     }
  }
  ...
}

As you can see, I want each file thats in the directory to be inside of the directory. How can I go about refactoring this function in order to obtain the output I desire?

What about (SPL) Directory Iterator

function getFileLists($dir) {
    $rec = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);
    $ar = array();
    foreach ($rec as $splFileInfo) {
       $path = $splFileInfo->isDir()
             ? array($splFileInfo->getFilename() => array())
             : array($splFileInfo->getFilename());

       for ($depth = $rec->getDepth() - 1; $depth >= 0; $depth--) {
           $path = array($rec->getSubIterator($depth)->current()->getFilename() => $path);
       }
       $ar = array_merge_recursive($ar, $path);
    }
    return $ar;
}
print "<pre>";
print_r(getFileLists("/Library/WebServer/Documents/am-web/"));

Personally I like to keep it simple, plucked this one out of my personal library, possible not the best answer but it get the trick done

<?php
function glob_recursive($dir, $recursive = false) {
    $files = [];
    foreach (glob($dir  . DIRECTORY_SEPARATOR . '*') as $object) {
        if (in_array($object, ['.', '..'])) {
            continue;
        }
        $file = [];
        $file['name'] = $object;
        if (false !== $recursive && is_dir($object)) {
            $file['files'] = glob_recursive($object);
        }
        $files[] = $file;
    }
    return $files;
}
$dir = __DIR__;
$list = glob_recursive($dir);

A simple but complete example which uses DirectoryIterator to list all of the files in a directory and recursion to avoid having to mangle the recursive iterator data to give all the info required. The thing I like about DirectoryIterator is that this gives an object where you can extract a lot of the files details (such as last modified time using getMTime() ).

function buildTree ( $path )    {
    $paths = [];
    foreach (new DirectoryIterator ($path) as $file) {
        if ( !$file->isDot() )   {
            $newFile = ["name" => $file->getRealPath(),
                "last_modified" => $file->getMTime()];
            if ($file->isDir()) {
                $newFile["files"] = buildTree($file->getRealPath());
            }
            $paths[] = $newFile;
        }
    }
    return $paths;
}
$paths = buildTree($root);

I would remove the "recursive" parameter and try something like this:

function getFileLists($dir)
{
    echo "Processing $dir\n";

    // retrieve an array of all the directories and files in a certain directory
    $retval = [];
    if (substr($dir, -1) !== "/") {
        $dir .= "/";
    }
    $d = @dir($dir) or die("unable to open {$dir} for reading, permissions?");
    while (false !== ($entry = $d->read())) {
        if ($entry{0} === ".") {
            continue;
        }
        if (is_dir("{$dir}{$entry}")) {
            $retval[] = [
                'name'          => "{$dir}{$entry}",
                'last_modified' => filemtime("{$dir}{$entry}"),
                //recursive call 
                'files'         => array_merge($retval, getFileLists("{$dir}{$entry}/"))
            ];
        } elseif (is_readable("{$dir}{$entry}")) {
            $retval[] = [
                'name'          => "{$dir}{$entry}",
                'last_modified' => filemtime("{$dir}{$entry}")
            ];
        }
    }
    $d->close();

    return $retval;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM