Forum Moderators: coopster
I'm trying to write a script that will search all folders within a certain folder and list them in date order with the newest at the top, and list the folders within those folders. Only problem, its repeating the folders from all the folders, and so creates a load of broken links. I bet that didnt make any sense ;)
Basically..
Folder
index.php
And that index.php is finding the folders inside 'Folder', so
Folder
..sub-folder1
....sub-subfolder1
....sub-subfolder2
..sub-folder2
....sub-subfolder3
....sub-subfolder4
However, the script is listing ....sub-subfolders 1 and 2 along with 3 and 4 for ..subfolder2, which isnt right.
....sub-subfolders 1 and 2 should only appear under ..sub-folder1 etc etc..
Heres the script. Yes i know i wrote curren instead of current in the variable names ;) As theres a script later on in the page that does the same thing but for archived stuff (hence the year as the folder), so i did that to avoid variable conflicts.
<?php
// list folders
// Your start directory
$currentpath = '2009/';
$currentweeds = array('.', '..', 'js', 'css', 'img');
$currentdirectories = array_diff(scandir($currentpath), $currentweeds);
rsort($currentdirectories); // reverse the order, so latest is at top
foreach($currentdirectories as $currentvalue)
{
if(is_dir($currentpath.$currentvalue))
{
echo "<h3>$currentvalue</h3>";
echo "<div class=\"subShowHide\">";
// list folders
// Your start directory
$currendir = "2009/$currentvalue";
if ($handle = opendir($currendir)) {
while (false !== ($currenfile = readdir($handle))) {
if ($currenfile != "." && $currenfile != ".." && $currenfile !="Testfolder" && $currenfile !="img" && $currenfile !="js" && $currenfile !="css") {
if (is_dir("$currendir/$currenfile"))
$folder_array[] = $currenfile;
else
$folder_array[] = $currenfile;
}
}
closedir($handle);
}
if ($folder_array) {
rsort ($folder_array);
foreach ($folder_array as $currenfile) {
echo "<a href='$currendir/$currenfile'>$currenfile</a><br />";
}
}
// ending brackets from first folder find
echo "</div>";
}
}
?>
$folder_arraywhe[] = $currenfile;
if no key is specified, the maximum of the existing integer indices is taken, and the new key will be that maximum value plus 1. If no integer indices exist yet, the key will be 0 (zero).