Auto generate web server index pages
In the olden days directory listings were switched on by default. This meant that web servers needed an index.html (or equivalent) in every directory in order to hide the contents from prying eyes. A working web server nearly always has temporary, spare or backup files under the web root and we don't want the general public looking at them. It only takes one novice user to rename database_login.php
to database_login.bak
and you are in a whole world of pain.
These days many web servers switch off the directory indexer by default. When you have your own web site you occasionally come across the problem of how to give a directory listing. In some circumstances, you may only wish to show an eclipsed list of the files in the directory and in other cases you may not care. Most of the time you just want to make a directory and its contents visible.
There are several different ways of displaying a directory listing. They each have advantages and disadvantages.
Generating a static page
Advantages: Very quick to generate, simple, minimum cost on server for displaying, you can selectively remove entries, flexible. Disadvantages: Needs to be regenerated each time there is a change in the directory's contents.
Below are 2 shell script listings that will generate a basic web page. The first example generates an index page containing pictures from a directory's contents. $TITLE is the page title and the heading title. You have to love the Unix set of power tools! Microsoft is still fumbling around in the dark when it comes to command line tools... anyway do a directory listing - take out the index file already created - place each file (from the listing) and embed it into an HTML IMaGe XML tag and write the directory name next to it - then close the page.
You could add this to a command file if it is something you do often. You could also add your own style sheet to the header or add customer branding if it is going to be used externally.
#!/bin/sh
#
#
TITLE="BigSoft Static Index Example"
echo "" > index.html
echo "$TITLE " >> index.html
echo "" >> index.html
echo "$TITLE
" >> index.html
ls \
| grep -v index.html \
| sed 's/.*/
&/' \
>> index.html
echo "" >> index.html
For links to documents:
#!/bin/sh
#
# (c) 2008 BigSoft Limited
# Please retain this copyright message
# Software comes AS IS and without warrently
#
TITLE="BigSoft Static Index Example"
echo "" > index.html
echo "$TITLE " >> index.html
echo "" >> index.html
echo "$TITLE
" >> index.html
ls \
| grep -v index.html \
| sed 's/.*/& <\/a>
/' \
>> index.html
echo "html
In a PHP file
Advantages: Simple, dynamically created. Disadvantages: Removing specific entries is trickier if the requirements change.
<?php
#
# (c) 2008 BigSoft Limited
# Please retain this copyright message
# Software comes AS IS and without warrently
#
$title = "BigSoft Limited PHP Indexer";
echo "$title ";
echo "$title
";
$dir = opendir(".");
while (($file = readdir($dir)) !== false)
{
if ($file[0] == ".")
continue;
echo "$file";
}
closedir($dir);
echo "";
?>
Let the web server do the work
Advantages: The least fuss. Disadvantages: Inflexible.
In the example below I will use Apache because (in my opinion) it is still the best and most configurable web server. Apache's configuration allows you to set options in a global configuration or configure special settings for an individual directory. In our case we want to configure a particular directory to allow visitors to see it's contents. This is where .htaccess
access files come in.
The .htaccess
file will override the default behaviour of the web server for just that directory.
Options +Indexes
IndexOptions +FoldersFirst
IndexOptions +FancyIndexing
IndexOptions +ScanHTMLTitles
IndexIgnore .htaccess *.bak *~ CVS
The .htaccess
file is equivalent to the the main configuration directive:
Options +Indexes
IndexOptions +FoldersFirst
IndexOptions +FancyIndexing
IndexOptions +ScanHTMLTitles
IndexIgnore .htaccess *.bak *~ CVS
The Options option defines a set of extra options relating to characteristics in the Directory directive. The +
means switch on Indexes.
The Indexes has several sub options.
FoldersFirst writes all the directory names first. FancyIndexing writes additional information, like size and modified date. ScanHTMLTitles is really cool! If the file is a web page, Apache looks for the
stands the .htaccess
spread out like above or compacted as below.
Options +Indexes
IndexOptions +FoldersFirst +FancyIndexing +ScanHTMLTitles
IndexIgnore .htaccess *.bak *~ CVS
4 comments
Comment from: Andrew [Visitor]

Comment from: Wiboon Warasittichai [Visitor]

Comment from: davidnewcomb [Member]

Comment from: Dretoot [Visitor]

Form is loading...
Thank you - I like option 2 for php. Is there an easy way to incorporate the following code to sort the index page files by date? This would be useful for sites to list the most recent files near the top.
I see there is code like this but it’s not complete (from php.net/opendir ):
[codeblock lang="php” line="1″] $content_array = array();
//set current working directory $dirname = “C:\temp";
//Load Directory Into Array $handle=opendir($dirname); $i=0; while ($file = readdir($handle)) if ($file != “.” && $file != “..") {
} //close the directory handle closedir($handle);
// these lines sort the contents of the directory by the date foreach($content_array as $res)
array_multisort($sortAux, SORT_ASC, $content_array); [/codeblock]