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
tag and writes it as a comment. This option can add quite a lot of extra load on your server, so use it sparingly.
IndexIgnore does not display files that match any given regular expression.
stands the .htaccess
spread out like above or compacted as below.
Options +Indexes
IndexOptions +FoldersFirst +FancyIndexing +ScanHTMLTitles
IndexIgnore .htaccess *.bak *~ CVS