Forum Moderators: coopster

Message Too Old, No Replies

finding list of folders within folders

         

surrealillusions

9:00 pm on Jan 21, 2009 (gmt 0)

10+ Year Member



Hi all,

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>";
}
}
?>

surrealillusions

10:20 pm on Jan 22, 2009 (gmt 0)

10+ Year Member



anyone? i really need to know what is going wrong... :(

phranque

12:21 am on Jan 23, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



maybe i missed it but i don't see where you are reinitializing folder_array for each directory.

surrealillusions

7:50 pm on Jan 23, 2009 (gmt 0)

10+ Year Member



is that not this line?

foreach ($folder_array as $currenfile) {

phranque

1:25 pm on Jan 24, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



your foreach is making an assignment to $currenfile and leaving the array untouched.
when you do this:
$folder_arraywhe[] = $currenfile;

you are appending values to the existing array:
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).

- PHP: Arrays - Manual [php.net]