Get the Hierarchy of a Directory with PHP

Get the hierarchy of a directory with PHP

function dir_contents_recursive($dir) {
// open handler for the directory
$iter = new DirectoryIterator($dir);

foreach( $iter as $item ) {
// make sure you don't try to access the current dir or the parent
if ($item != '.' && $item != '..') {
if( $item->isDir() ) {
// call the function on the folder
dir_contents_recursive("$dir/$item");
} else {
// print files
echo $dir . "/" .$item->getFilename() . "<br>";
}
}
}
}

Listing all the folders subfolders and files in a directory using php

function listFolderFiles($dir){
$ffs = scandir($dir);

unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);

// prevent empty ordered elements
if (count($ffs) < 1)
return;

echo '<ol>';
foreach($ffs as $ff){
echo '<li>'.$ff;
if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
echo '</li>';
}
echo '</ol>';
}

listFolderFiles('Main Dir');

Get directory structure from FTP using PHP

Start here: PHP FTP recursive directory listing.

You just need to adjust the code to:

  1. the DOS-style listing you have from your FTP server (IIS probably) and
  2. to collect only the folders.
function ftp_list_dirs_recursive($ftp_stream, $directory)
{
$result = [];

$lines = ftp_rawlist($ftp_stream, $directory);
if ($lines === false)
{
die("Cannot list $directory");
}

foreach ($lines as $line)
{
// rather lame parsing as a quick example:
if (strpos($line, "<DIR>") !== false)
{
$dir_path = $directory . "/" . ltrim(substr($line, strpos($line, ">") + 1));
$subdirs = ftp_list_dirs_recursive($ftp_stream, $dir_path);
$result = array_merge($result, [$dir_path], $subdirs);
}
}
return $result;
}

Deep recursive array of directory structure in PHP

I recommend using DirectoryIterator to build your array

Here's a snippet I threw together real quick, but I don't have an environment to test it in currently so be prepared to debug it.

$fileData = fillArrayWithFileNodes( new DirectoryIterator( '/path/to/root/' ) );

function fillArrayWithFileNodes( DirectoryIterator $dir )
{
$data = array();
foreach ( $dir as $node )
{
if ( $node->isDir() && !$node->isDot() )
{
$data[$node->getFilename()] = fillArrayWithFileNodes( new DirectoryIterator( $node->getPathname() ) );
}
else if ( $node->isFile() )
{
$data[] = $node->getFilename();
}
}
return $data;
}

List all the files and folders in a Directory with PHP recursive function

Get all the files and folders in a directory, don't call function when you have . or ...

Your code :

<?php
function getDirContents($dir, &$results = array()) {
$files = scandir($dir);

foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}

return $results;
}

var_dump(getDirContents('/xampp/htdocs/WORK'));

Output (example) :

array (size=12)
0 => string '/xampp/htdocs/WORK/iframe.html' (length=30)
1 => string '/xampp/htdocs/WORK/index.html' (length=29)
2 => string '/xampp/htdocs/WORK/js' (length=21)
3 => string '/xampp/htdocs/WORK/js/btwn.js' (length=29)
4 => string '/xampp/htdocs/WORK/js/qunit' (length=27)
5 => string '/xampp/htdocs/WORK/js/qunit/qunit.css' (length=37)
6 => string '/xampp/htdocs/WORK/js/qunit/qunit.js' (length=36)
7 => string '/xampp/htdocs/WORK/js/unit-test.js' (length=34)
8 => string '/xampp/htdocs/WORK/xxxxx.js' (length=30)
9 => string '/xampp/htdocs/WORK/plane.png' (length=28)
10 => string '/xampp/htdocs/WORK/qunit.html' (length=29)
11 => string '/xampp/htdocs/WORK/styles.less' (length=30)

Build directory tree from flat array of paths in PHP

You could do this in two steps:

  • Create a hierarchy of associative arrays, where the labels are the keys, and nested arrays correspond to children.
  • Transform that structure to the target structure

Code:

function buildTree($branches) {
// Create a hierchy where keys are the labels
$rootChildren = [];
foreach($branches as $branch) {
$children =& $rootChildren;
foreach($branch as $label) {
if (!isset($children[$label])) $children[$label] = [];
$children =& $children[$label];
}
}
// Create target structure from that hierarchy
function recur($children) {
$result = [];
foreach($children as $label => $grandchildren) {
$node = ["label" => $label];
if (count($grandchildren)) $node["children"] = recur($grandchildren);
$result[] = $node;
}
return $result;
}
return recur($rootChildren);
}

Call it likes this:

$tree = buildTree($branches);

The above will omit the children key when there are no children. If you need to have a children key in those cases as well, then just remove the if (count($grandchildren)) condition, and simplify to the following version:

function buildTree($branches) {
// Create a hierchy where keys are the labels
$rootChildren = [];
foreach($branches as $branch) {
$children =& $rootChildren;
foreach($branch as $label) {
if (!isset($children[$label])) $children[$label] = [];
$children =& $children[$label];
}
}
// Create target structure from that hierarchy
function recur($children) {
$result = [];
foreach($children as $label => $grandchildren) {
$result[] = ["label" => $label, "children" => recur($grandchildren)];
}
return $result;
}
return recur($rootChildren);
}


Related Topics



Leave a reply



Submit