PHP recursive scanning a folder into multiple arrays (subfolders and files)

At the moment, I'm a little lost. My goal is to recursively scan a folder with subfolders and images in each subfolder in order to get it in a multidimensional array, and then to be able to analyze each subfolder with its containing images.

I have the following startup code, which basically scans every subfolder containing files, and is simply lost to get it into multiple arrays.

$dir = 'data/uploads/farbmuster'; $results = array(); if(is_dir($dir)) { $iterator = new RecursiveDirectoryIterator($dir); foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) { if($file->isFile()) { $thispath = str_replace('\\','/',$file->getPath()); $thisfile = utf8_encode($file->getFilename()); $results[] = 'path: ' . $thispath. ', filename: ' . $thisfile; } } } 

Can someone help me?

Thanks in advance!

+4
source share
3 answers

You can try

 $dir = 'test/'; $results = array(); if (is_dir($dir)) { $iterator = new RecursiveDirectoryIterator($dir); foreach ( new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file ) { if ($file->isFile()) { $thispath = str_replace('\\', '/', $file); $thisfile = utf8_encode($file->getFilename()); $results = array_merge_recursive($results, pathToArray($thispath)); } } } echo "<pre>"; print_r($results); 

Output

 Array ( [test] => Array ( [css] => Array ( [0] => a.css [1] => b.css [2] => c.css [3] => css.php [4] => css.run.php ) [CSV] => Array ( [0] => abc.csv ) [image] => Array ( [0] => a.jpg [1] => ab.jpg [2] => a_rgb_0.jpg [3] => a_rgb_1.jpg [4] => a_rgb_2.jpg [5] => f.jpg ) [img] => Array ( [users] => Array ( [0] => a.jpg [1] => a_rgb_0.jpg ) ) ) 

Function used

 function pathToArray($path , $separator = '/') { if (($pos = strpos($path, $separator)) === false) { return array($path); } return array(substr($path, 0, $pos) => pathToArray(substr($path, $pos + 1))); } 
+10
source

A recursive DirectIterator scans recursively into a flat structure. To create a deep structure, you need a recursive function (calls) using DirectoryIterator . And if your current isDir () and file! isDot (), go into it, again calling the function with the new directory as arguments. And add a new array to your current set.

If you cannot handle this scream, I will give the code here. I have to document (now there are ninja comments), it's a little like that ... I try my luck in a lazy way, with instructions.

CODE

 /** * List files and folders inside a directory into a deep array. * * @param string $Path * @return array/null */ function EnumFiles($Path){ // Validate argument if(!is_string($Path) or !strlen($Path = trim($Path))){ trigger_error('$Path must be a non-empty trimmed string.', E_USER_WARNING); return null; } // If we get a file as argument, resolve its folder if(!is_dir($Path) and is_file($Path)){ $Path = dirname($Path); } // Validate folder-ness if(!is_dir($Path) or !($Path = realpath($Path))){ trigger_error('$Path must be an existing directory.', E_USER_WARNING); return null; } // Store initial Path for relative Paths (second argument is reserved) $RootPath = (func_num_args() > 1) ? func_get_arg(1) : $Path; $RootPathLen = strlen($RootPath); // Prepare the array of files $Files = array(); $Iterator = new DirectoryIterator($Path); foreach($Iterator as /** @var \SplFileInfo */ $File){ if($File->isDot()) continue; // Skip . and .. if($File->isLink() or (!$File->isDir() and !$File->isFile())) continue; // Skip links & other stuff $FilePath = $File->getPathname(); $RelativePath = str_replace('\\', '/', substr($FilePath, $RootPathLen)); $Files[$RelativePath] = $FilePath; // Files are string if(!$File->isDir()) continue; // Calls itself recursively [regardless of name :)] $SubFiles = call_user_func(__FUNCTION__, $FilePath, $RootPath); $Files[$RelativePath] = $SubFiles; // Folders are arrays } return $Files; // Return the tree } 

Test its output and identify it. You can do it!

+1
source

If you want a list of files with subdirectories, use (but change the name of the folder)

 <?php $path = realpath('yourfold/samplefolder'); foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename) { echo "$filename\n"; } ?> 
0
source

Source: https://habr.com/ru/post/1440777/


All Articles