?
Solved

a recursive directory php challenge

Posted on 2006-03-26
1
Medium Priority
?
287 Views
Last Modified: 2013-12-12
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");
?>
0
Comment
Question by:rblampain
1 Comment
 
LVL 2

Accepted Solution

by:
vorbe earned 2000 total points
ID: 16301240
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question