Good day to you all,
here I have a recursive directory lister that puts everything into an array.
Here is the code:
<?
function getDirectory($path = '.', $ignore = '') {
$dirTree = array ();
$dirTreeTemp = array ();
$ignore[] = '.';
$ignore[] = '..';
$dh = @opendir($path);
while (false !== ($file = readdir($dh))) {
if (!in_array($file, $ignore)) {
if (!is_dir("$path/$file")) {
$dirTree["$path"][] = $file;
} else {
$dirTreeTemp = getDirectory("$path/$file", $ignore);
if (is_array($dirTreeTemp))$dirTree = array_merge($dirTree, $dirTreeTemp);
}
}
}
closedir($dh);
return $dirTree;
}
$ignore = array('.htaccess', 'error_log', 'cgi-bin', 'php.ini', '.ftpquota');
$dirTree = getDirectory('Photos', $ignore);
?>
<pre>
<?
print_r($dirTree);
?>
</pre>
My question is how can I keep the same array and without looping again through the directories again list only the folder ?
If i can, how can I also list a text only the folder that does have folder in them and as a link the one that as files.
So when a link is press I can list the array under that folder
Thanks!
Peuplarchie:
My question is how can I keep the same array and without looping again through the directories again list only the folder ?
If i can, how can I also list a text only the folder that does have folder in them and as a link the one that as files.
So when a link is press I can list the array under that folder
Thanks!
A little bit of trial and error has resulted in the following for the lower part of your code.
What happens is:
[list][]the files are kept separate from the directories, so that they can each be sorted separately and then joined together.
[ ]only top-level directories are added to the list
[*]regardless of whether the directory is top-level or not, if it contains files then the top-level directory is linked
$path = '../../images';
$dirTree = getDirectory($path, $ignore);
$localDir = Array();
$linked = array();
foreach ($dirTree as $dir => $content) {
if ($dir === $path) {
$localFiles = $content;
} else {
$trimmed = str_replace($path, '', $dir);
if (!strpos($trimmed, '/', 1)) {
array_push($localDir, $trimmed);
}
if (count($dirTree[$dir]) > 0) {
$pathToFind = substr($trimmed, 0, strpos($trimmed, '/', 1));
if (!$pathToFind) {
$pathToFind = $trimmed;
}
$indexToLink = array_search($pathToFind, $localDir);
if (!$linked[$indexToLink]) {
$localDir[$indexToLink] = '<a href="list.php?dir='.$path.$pathToFind.'">'.$localDir[$indexToLink].'</a>';
}
$linked[$indexToLink] = TRUE;
}
}
}
sort($localDir);
sort($localFiles);
$localDir = array_unique(array_merge($localDir, $localFiles));
?>
<pre>
<?
print_r($dirTree);
print_r($localDir);
?>
</pre>
How do I implement it, i’m still trying ?
You will want to add your code (up until the dirtree part) above my own code.
You will also want to use your own path.
$path = ‘Photos’ instead of $path = ‘…/…/images’
I have not implemented the part that captures the path from the clicked-on directory.
I have to leave shortly so that can be left as an exercise for you to complete.