Zend - The PHP Company




HTTP

Add Code


Fake Apache DirectoryIndex Listing  

Type: code fragment
Added by: flight553
Entered: 10/03/2001
Last modified: 03/12/2000
Rating: - (fewer than 3 votes)
Views: 20567
If you want to have a Header and Footer on every page, including default apache directoryindexes, then you can use this script to generate a directory index instead of letting Apache do it. Combined with php's auto_prepend_file and auto_append_file, you will get your headers and footers on all documents served. Without this you would be trying to use the apache directives for HeaderName and ReadmeName, but they only work if the file to use is inside the directory that apache is giving a listing of (you can't spacify a URL or absolute path for those 2 directives in a top level .htaccess file). This is good if you have a giant site with a ton of subdirectories and you dont feel like going around creating ReadmeName and HeaderName files for Apache to use. The down side if that all docs must be parsed as php for auto_append and auto_prepend to work. The upside is that PHP is great. This code was modified from source provided by http://www.webaugur.com/wares/autoindex written by dave at webaugur. com


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
        "http://www.w3.org/TR/REC-html40/strict.dtd">
<html lang="en">
<head>
<title>Directory Index Listing</title>

<STYLE TYPE="text/css"><!-- 
TD,TABLE  {      
color: black;      
font-size: 11px;      
font-family: courier,monospace,sans-serif;  }  
--></STYLE>

</head>
<body bgcolor=gray alink=red link=black vlink=purple>
<?php

Function fncLinkDir$dirname )
  {
   global 
$PHP_SELF;

  
/* Get rid of those stupid filenames  */
  /* Was I sleepy when I did this?  Alrighty then.  */

/*
  if ( is_file( $DOCUMENT_ROOT.$REQUEST_URI ) )
    {
        $REQUEST_URI = ereg_replace( $mefile = strrchr( $REQUEST_URI, "/" ), "/", $REQUEST_URI );
    };
*/

  /* Open Correct Directory for reading.  */
  
if( !$dirid = @opendir$dirname ) )
    {
        print 
" &lt; - Unable to Open Directory";
        return 
1;
    };

        
/* Read the contents of the directory one by one  */
        
while ($entry = @readdir($dirid)) 
          {
          
/*  Begin File Exclusion; 
                 for long lists use an array and while loop 
                 to save you some editing time.  PHP 4 races through loops.
           */
              
if("/$entry== $PHP_SELF) continue; 
              if(
$entry == "HEADER" || $entry == "README") continue;
             
// apache would have displayed the contents of those files
             // in a default directory listing. We have them covered with
             // php_value auto_prepend_file and auto_append_file 

              /* Do not list hidden files; begins with .  
                  Why: Indexing . will cause infinite loop and eventually 
                  a stack overfrow.
               */
              
if ( strpos$entry"." ) === )
               {
                   continue;
               }

              
/* Do not list Microsoft Frontpage junk, either.  */
              
if ( strpos$entry"_vti_" ) === )
               {
                   continue;
               }
           
/* End File Exclusion  */

              /* Add this entry to the listing */
              
$dirEntries[] = $entry;
          }

          
/* Sort listing alphabetically then reset to start */
          
sort$dirEntries );
          
reset$dirEntries );


          
/* Walk through the current directory  */
          
$i 0;
          while( 
$dirEntries[$i] )
            {
              
/* Assign a short name  */
              
$fileName =  $dirEntries[$i];
              
$fileNameShort $dirEntries[$i];

              echo 
"<tr>n";

              if( 
is_dir$dirname $fileName ) )
                
/* It is a directory structure  */
                
{
                    
// HTML, Open directory list item  
                    
print "<td align=center><img src=/icons/folder.gif border=0></td>n".
                          
"<td colspan=3><a href="$fileName">$fileNameShort/</a></td>n";

                    
// Recurse into subdirectory  
                    // fncLinkDir( $fileName );
                   // if you have a butt-load of subdirs and files below, this will not be so great
                    // HTML, Close the directory list item  
                
}
              else  
                
/* It is some type of file */
                

                    if(
eregi(".([a-z0-9]+)$"$fileNameShort$match)) {
                         
// echo "$match[1]";
                         // my imitation of Apache's AddIcon
                         // this works if you have an /icon alias in your httpd.conf
                         
switch(strtolower($match[1])) {
                            case 
"pdf"  :               $img "pdf.gif"; break;
                            case 
"ps"   :               $img "ps.gif"; break;
                            case 
"gz"   : case "zip" :  $img "compressed.gif"; break;
                            case 
"html" : case "htm" :  $img "layout.gif"; break;
                            case 
"gif"  : case  "png" $img "image2.gif"; break;
                            case 
"jpg"  : case  "bmp" $img "image3.gif"; break;
                            case 
"mp3"  : case "wav" :  case "mid" $img "sound2.gif"; break;
                            case 
"txt"  :               $img "text.gif"; break;
                            case 
"php"  : case "pl" :   $img "script.gif"; break;
                            case 
"c"    :               $img "c.gif"; break;
                            case 
"py"   :               $img "p.gif"; break;
                            case 
"exe"  :               $img "binary.gif"; break;
                            case 
"mov"  :               $img "movie.gif"; break;
                            case 
"tar"  :               $img "tar.gif"; break;
                            default     :               
$img "unknown.gif"; break;
                            } 
// end switch
                         
// end case matched file extension
                    
else $img "text.gif";  // assume text

                    
echo "<td align=center><img src=/icons/$img border=0></td>n";
                    echo 
"<td><a href="$fileName">$fileNameShort</a></td>n";
                    echo 
"<td align=right>"date("j-M-Y h:ia"filemtime($dirname $fileName)) . "</td>n";
                    echo 
"<td align=right>";

                    
$sz filesize($dirname $fileNameShort);
//                    if($sz > 1048576)
//                         echo number_format((double)$sz/1048576 ,1) ." M";
//                  else 
                    
if($sz 1024)
                         echo (int)(
$sz/1024) ." k";
//                    else echo "$sz bytes";
                    
else echo "< 1 k";

                    echo 
"</td>n";
                } 
// end case was a file

              
echo "</tr>n";

              
/* Increment the index else we loop forever, doh!  */
              
$i ++;
            } 
// end while


  /* Wait, we're finished? */
  
};

$indexuri explode("?"$REQUEST_URI);
print 
"<h2>Index of ".urldecode($indexuri[0])."</h2>n";
echo 
"<table cellpadding=2 cellspacing=0 border=0>n".
     
"<tr><td width=22>&nbsp;</td><td width=200>Name</td>".
     
"<td width=160>Last Modified</td>".
     
"<td width=110 align=center>Size</td></tr>n";

echo 
"<tr><td align=center><img src=/icons/back.gif border=0></td>".
     
"<td colspan=3><a href='../'>Parent Directory</a></td></tr>n";

fncLinkDir("/absolute/filesystem/path/to/yer/webdir".$REQUEST_URI);
echo 
"</table>";
?>
<hr>
</body>
<!-- my  .htaccess file that I use with this:
RemoveHandler .pl .cgi
# I want to serve all sourcecode. be careful, dont serve your username and password!
Options -ExecCGI
DirectoryIndex index.html index.htm default.html index.php index.php3 index.phtml /dirindex.php
# that last one above will force this script to takeover if no index file is found
HeaderName HEADER
ReadmeName README
# those 2 above don't do anything anymore. Apache would have used them
AddType application/x-httpd-php .html .htm
# must parse everything as php for these next 2 lines to work 
php_value auto_prepend_file /absolute/filesystem/path/to/yer/webdir/HEADER
php_value auto_append_file /absolute/filesystem/path/to/yer/webdir/README
-->
</html>


Usage Example


See the example


Rate This Script





Search



This Category All Categories