PHP Recursive Directory Path

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)

PHP recursive directory path

Try to use RecursiveIteratorIterator in combination with RecursiveDirectoryIterator

$path = realpath('/path/you/want/to/search/in');

$objects = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::SELF_FIRST);

foreach($objects as $name => $object){
if($object->getFilename() === 'work.txt') {
echo $object->getPathname();
}
}

Additional reading:

  • http://www.phpro.org/tutorials/Introduction-to-SPL.html

PHP: List all files in a folder recursively and fast

You could use the RecursiveDirectoryIterator - but I doubt, it's faster than a simple recusive function.

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/folder'));
foreach ($iterator as $file) {
if ($file->isDir()) continue;
$path = $file->getPathname();
}

How to recursively iterate through files in PHP?

PHP has the perfect solution for you built in.

Example

// Construct the iterator
$it = new RecursiveDirectoryIterator("/components");

// Loop through files
foreach(new RecursiveIteratorIterator($it) as $file) {
if ($file->getExtension() == 'html') {
echo $file;
}
}

Resources

  • DirectoryIterator - Manual

Create file and folders recursively

For each of this paths you'll have to specific whether it is a file or a directory. Or you could make your script assume, that the path is pointing to a file when the basename (the last part of the path) contains a dot.

To create a directory recursively is simple:

mkdir(dirname($path), 0755, true); // $path is a file
mkdir($path, 0755, true); // $path is a directory

0755 is the file permission expression, you can read about it here: http://ch.php.net/manual/en/function.chmod.php

PHP - Listing all directories and sub-directories recursively in drop down menu

RecursiveDirectoryIterator should do the trick. Unfortunately, the documentation is not great, so here is an example:

$root = '/etc';

$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);

$paths = array($root);
foreach ($iter as $path => $dir) {
if ($dir->isDir()) {
$paths[] = $path;
}
}

print_r($paths);

This generates the following output on my computer:

Array
(
[0] => /etc
[1] => /etc/rc2.d
[2] => /etc/luarocks
...
[17] => /etc/php5
[18] => /etc/php5/apache2
[19] => /etc/php5/apache2/conf.d
[20] => /etc/php5/mods-available
[21] => /etc/php5/conf.d
[22] => /etc/php5/cli
[23] => /etc/php5/cli/conf.d
[24] => /etc/rc4.d
[25] => /etc/minicom
[26] => /etc/ufw
[27] => /etc/ufw/applications.d
...
[391] => /etc/firefox
[392] => /etc/firefox/pref
[393] => /etc/cron.d
)

Directory path to recursive array with children relation

You don't need to use a recursive function to build the structure, and if you use a keyed array it becomes much simpler to work with as you don't have to search arrays for entries with a certain directory_name:

$paths = [
'parent/first child',
'parent/second',
'parent/second/with-another-children',
'parent/third/another children',
'parent/last children'
];

$out = [];

foreach($paths as $path) {
$parts = explode('/', $path);
$cur = &$out;
foreach($parts as $part) {
if(!key_exists($part, $cur)) {
$cur[$part] = [ 'children' => []];
}
$cur = &$cur[$part]['children'];
}
unset($cur);
}

var_dump($out);

Output:

array(1) {
["parent"]=>
array(1) {
["children"]=>
array(4) {
["first child"]=>
array(1) {
["children"]=>
array(0) {
}
}
["second"]=>
array(1) {
["children"]=>
array(1) {
["with-another-children"]=>
array(1) {
["children"]=>
array(0) {
}
}
}
}
["third"]=>
array(1) {
["children"]=>
array(1) {
["another children"]=>
array(1) {
["children"]=>
array(0) {
}
}
}
}
["last children"]=>
array(1) {
["children"]=>
array(0) {
}
}
}
}
}

Recursively search Directory by name and return path

Two main problems:

  1. You don't provide a pattern for glob that will return any subdirectories
  2. You don't return the result of the recursive call

which means it will never find your target (unless your target happens to be the directory you start in), so it will never return.


Try making the following changes

public function LocalDirSearch($clouse) {
$path = $clouse[0];
$search = $clouse[1];
$result = $clouse[2];

// add a wildcard to the glob pattern so it will match everything under $path
$dirs = glob($path . '/*', GLOB_ONLYDIR);

foreach ($dirs as $dir) {
if (basename($dir)==$search){
return $dir;
}

// return the result of the recursive call
if ($subdir = $this->LocalDirSearch([$dir, $search, $result])) {
return $subdir;
}
}
}

Incidentally, break is unnecessary after return, and $result does not appear to be used in the function.



Related Topics



Leave a reply



Submit