Deep recursive array of directory structure in PHP

后端 未结 5 2257
有刺的猬
有刺的猬 2020-11-27 18:22

I\'m trying to put some folders on my hard-drive into an array.

For instance, vacation pictures. Let\'s say we have this structure:

  • Set 1
    • Ite
5条回答
  •  情深已故
    2020-11-27 19:26

    So 6 years later....

    I needed a solution like the answers by @tim & @soulmerge. I was trying to do bulk php UnitTest templates for all of the php files in the default codeigniter folders.

    This was step one in my process, to get the full recursive contents of a directory / folder as an array. Then recreate the file structure, and inside the directories have files with the proper name, class structure, brackets, methods and comment sections ready to fill in for the php UnitTest.

    To summarize: give a directory name, get a single layered array of all directories within it as keys and all files within as values.

    I expanded a their answer a bit further and you get the following:

    function getPathContents($path)
    {
         // was a file path provided?
         if ($path === null){
            // default file paths in codeigniter 3.0
            $dirs = array(
                './models',
                './controllers'
            );
        } else{
            // file path was provided
            // it can be a comma separated list or singular filepath or file
            $dirs = explode(',', $path);
        }
        // final array
        $contents = array();
        // for each directory / file given
        foreach ($dirs as $dir) {
            // is it a directory?
            if (is_dir($dir)) {
                // scan the directory and for each file inside
                foreach (scandir($dir) as $node) {
                    // skip current and parent directory
                    if ($node === '.' || $node === '..') {
                        continue;
                    } else {
                        // check for sub directories
                        if (is_dir($dir . '/' . $node)) {
                            // recursive check for sub directories
                            $recurseArray = getPathContents($dir . '/' . $node);
                            // merge current and recursive results
                            $contents = array_merge($contents, $recurseArray);
                        } else {
                            // it a file, put it in the array if it's extension is '.php'
                            if (substr($node, -4) === '.php') {
                                // don'r remove periods if current or parent directory was input
                                if ($dir === '.' || $dir === '..') {
                                    $contents[$dir][] = $node;
                                } else {
                                    // remove period from directory name 
                                    $contents[str_replace('.', '', $dir)][] = $node;
                                }
                            }
                        }
                    }
                }
            } else {
                // file name was given
                $contents[] = $dir; 
            }
        }
        return $contents;
    }
    

提交回复
热议问题