a recursive directory php challenge

Casual PHP coder has a question about a variation of recursive directory listing.
The variations are:
1) The real directory names are hidden from the user and a translation table is used:
example: the user names a directory as: "The quick brown fox jumps over the lazy black dog." which another script has translated as a 4 letter directory name like "abcd" which is only accessible through PHP (apache2).
2) Directories contains files names that are an hexadecimal translation of a 4 bytes time stamp:
example: the user does not have a particular name for a file and prefers to refer to it by the time (and date) it was created. A 4 bytes time stamp is obtained and translated to an
hexadecimal file name (8 bytes) to avoid unacceptable file names.

The script bellow works including the translation described above (neglecting point 2) except for one problem, the display is not sorted and it needs to be.
This seems to be a bigger hurdle than anticipated, I've spent a few days trying to work it out but my experience in PHP is very basic and my resulting script is getting very complicated.

Any help in solving point 1 sorted in alphabetical order is most welcome, I think point 2 shouldn't really be a problem but any hint is welcome.

Thank you for your help.

<?php
   $trans=file('zession/.transtbl.data');      # read translation file
   $trans2 = array();
foreach ($trans as $value) {             # value is full line from transbl.data    
     $parts=explode("=",$value);      # parts[0] is name, parts[1] is path      
     $trans2[$parts[1]]=$parts[0];      # path = name
}

function make_tree($address){ #0
  global $trans2, $name;
  @$dir = opendir($address);
    if(!$dir){ return 0; }  #1 #1
      print "<ol>";
        while(false !== ($entry = readdir($dir))){ #2
          if(is_dir("$address/$entry") && ($entry != ".." && $entry != ".")){ #3
            ?><li><input type="radio" name="directory[]" value="<?php echo $address ."/". $entry; ?>"><?php
            # full path = $address ."/".$entry
            $path=$address ."/". $entry."/";
              foreach ($trans2 as $key => $value) { #4
                $key = rtrim($key, "\x00..\x20");
                  if ($path==$key) { #5
                    $name=$value;
                  } #5
              } #4
            echo "$name";            # display the name
            $name="";                  # then clear it (must do)
            make_tree("$address/$entry");
          } #3
        } #2
  print "</ol>";
} #0
make_tree("zession");
?>
rblampainAsked:
Who is Participating?
 
vorbeCommented:
The main problem here is that you display the list on the fly, as you traverse the directory entries. To sort the output you need to read all directory entries in an array and then sort them by their values.

after position #1 (the line with that comment) there should be something like this:

  print "<ol>";
  $dirs = Array();
  while (false !== ($entry = readdir($dir))) { #2
    if (is_dir("$address/$entry") && ($entry != ".." && $entry != ".")) { #3
      $path = $address . "/" . $entry . "/";
      foreach ($trans2 as $key => $value) { #4
        $key = rtrim($key, "\x00..\x20");
        if ($path == $key) { #5
          $name = $value;
        } #5
      } #4
      # register it in the array
      $dirs[$address . "/" . $entry] = $name;
  } # 2

  # now sort the array by values
  asort($dirs);
 
  #and display them
  foreach ($dirs as $path => $name) {
    ?><li><input type="radio" name="directory[]" value="<php echo $path; ?>"><?php
    echo(htmlspecialchars($name)); # don't forget htmlspecialchars
    make_tree($path); # now enter recursion
  }

  print "</ol>";

} #0

Hope it helps
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.