a recursive directory php challenge

Posted on 2006-03-26
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.

   $trans=file('zession/');      # read translation file
   $trans2 = array();
foreach ($trans as $value) {             # value is full line from    
     $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
                  } #5
              } #4
            echo "$name";            # display the name
            $name="";                  # then clear it (must do)
          } #3
        } #2
  print "</ol>";
} #0
Question by:rblampain
    1 Comment
    LVL 2

    Accepted Solution

    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
      #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

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now