Page 1 of 1

readdir subdirectorys

Posted: Thu Jun 11, 2009 2:10 am
by fredwacko
Ok so Im trying to build an indexer type of file and so far I have it going pretty well except if there is a directory in the folder thats being searched then it will display the link for the folder (which doesnt work) and then move on. Id like it to search in the subdirectorys as well. Ive thought of a couple ways this could be implemented but as I am a php novice, I need help.

Option 1: read the dir then if its a directory search inside it and execute script (to write files to a txt file), else execute script and then repeat until folder is done.
Option 2: read the dir and store all the folder names as variables and record how many ie

Code: Select all

$folder1 = a_folder, $folder2 = another_folder, $total = 2
and get all the files listed, then go back and loop through all the found folders.

I dont really care which way, any will do although most efficient would be nice.
This is what Ive got so far:

Code: Select all

 
 
<?php
 if ($handle = opendir('files/folder')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
      {
            $thelist .= '<a href="files/folder/'.$file.'">'.$file.'</a>'.'<br>';
          }
       }
  closedir($handle);
  }
?>
Folder added<br>
<?php
$fh = fopen("main/lists/folder/files.php", "w+");
fwrite($fh, $thelist);
fclose($fh);
?>
<?php
 $thelist = '';
?>
 
 
Id like the folders inside "folder" to be listed in their own "main/lists/folder/foldername.php" if possible as it would help with alterations to the core layout later on.
Can anyone help?