When a directory listing is generated through a script, the server gathers the information related to the files inside the listed directory and packs it in a special string. Then the script is executed and the special string is POSTed to it inside a variable called files. The server also POSTs the directory virtual path in a variable which name is path as well as an URL encoded (escaped) version of the virtual path in a variable named encoded_path.

This special string passed in the files variable is of the form:


→ represents the tabulation character (ASCII code 9 - \t) and [CR] represents the carriage return character (ASCII code 13 - \n). The tuple (namei, urli, sizei, datei, MIME-typei) is the information related to the ith file in the listing.

The name and the URL of every file are UTF-8 encoded. The size is in bytes. The date is conforming to the format YYYY-MM-DD hh:mm:ss which is equivalent in strftime semantics to %Y-%m-%d %H:%M:%S.

The MIME type of a directory is empty, i.e. it is equal to the empty string. The name of a directory contains always a trailing slash.

The script should split the files variable value on the carriage return characters then split each line on the tabulation character to have the information related to each file.

Example 8-2. A very simple directory listing script (PHP version)

       <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
           Index of <?php echo $_POST['path']; ?>
       <TABLE BORDER="0">
           <TD>MIME Type</TD>
           /* Split and get the lines */
           $lines = explode("\n", $_POST['files']);
           /* For each line do... */
           foreach ($lines as $line)
             /* Split the line and get the file information */
             list($name, $url, $size, $date, $mimetype) = explode("\t", $line);
             if ($mimetype == "")
               $mimetype = "Directory";
             echo "<TR><TD><A HREF=\"$url\">" . htmlentities($name) .

Example 8-3. A very simple directory listing script (Python version)

   import cgi, string, os
   posted_data = cgi.FieldStorage()
   # Write the CGI header
   print "Content-Type: text/html; charset=utf-8"
   print "<HTML><HEAD>"
   print "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\">"
   print "<TITLE>Index of %s</TITLE></HEAD>" % posted_data["path"].value
   print "<TABLE BORDER='0'><TR><TD>Name</TD><TD>Size</TD><TD>Date</TD><TD>MIME Type</TD></TR>"
   # Split and get the lines
   lines = (posted_data["files"].value).split("\n")
   # for each line do...
   for line in lines:
           # Split the line and get the file information
           (name, url, size, date, mimetype) = line.split("\t")
           if (mimetype == ""):
                   mimetype = "Directory"
           print "<TR><TD><A HREF=\"%s\">%s</A></TD>" % ( url, cgi.escape(name) )
           print "<TD>%d</TD><TD>%s</TD><TD>%s</TD></TR>" % (int(size), date, mimetype)
   print "</TABLE></BODY></HTML>"