(Site Identification)

'Eye' Focus: Web Support Tutorials

Enabling Directory Browsing

Dilemma:

Q: I want to offer any of my files in a directory for easy access. I change the files there often and do not have time to update a web page to link to the files. Is there a way to simply provide a directory listing of all the files in a directory?


A: Directory browsing is turned off system-wide by default to allow site developers the privacy of their site structure and un-linked content. However, you do have the ability to selectively enable directory browsing in your site.


(to Top) Simple Solution

The method is simple. You need one file named: .htaccess
(That's a dot, followed by htaccess as the file name suffix, there is no prefix to this file name.)

In the file enter this one line of text:
Options +Indexes

That's all there is to it. This short line of text simply tells the Apache web server to allow directory browsing, thereby permitting public browsing of all contents within the directory where this file appears. Keep in mind, however, that this includes all sub-directories and files contained within that directory.

(to Top) Advanced Programming Method

If you are a bit more advanced in your programming skills then you might try a bit of customized PHP coding. Using the example shown below you could customize what contents to display from your directories, and even how to display it. What you can do is only limited by your imaginiation.

PHP Directory Listing Example:

A linked list of directory contents is automatically generated by this file, and will only show a desired list of available file types. (skip code)

<?php
// read this directory
$thisDir substr($_SERVER['PATH_TRANSLATED'],0,strrpos($_SERVER['PATH_TRANSLATED'],'/'));
$cntFile 0// start file counter, names that have an extension
$cntDir 0// start directory counter, names with no extension
$dlist ''// build the resulting list and then echo the final results
// 1st run: generate the list of files with no filename extension
if ($dir = @opendir($thisDir)) { // open a specified directory
    
$dlist .= "\n" '<h3>Directory/Other <small style="font-weight:500;">';
    
$dlist .= '(Items with no Filename Extension)</small></h3>' "\n" '<ul>';
    while (
$file = @readdir($dir)) { // read a name from the directory
        
if (strpos(' '.$file,'.') != && !strpos(' '.$file,'.')) { // if a dir
            
$dlist .= "\n" '<li>' htmlentities($file) . '</li>';
            
$cntDir += 1// increment the counter
        
}
    }
    if (
$cntDir 1$dlist .= "\n" '<div><small>(none)</small></div>';
    
$dlist .= "\n" '</ul>';
    
closedir($dir); // close the directory
}
// 2nd run: generate the list of files that have filename extensions
if ($dir = @opendir($thisDir)) { // open a specified directory
    
$dlist .= "\n" '<h3>Mime Type Files <small style="font-weight:500;">';
    
$dlist .= '(Items that have Filename Extensions)</small></h3>' "\n" '<ul>';
    while (
$file = @readdir($dir)) { // read a name from the directory
        
if (strpos($file,'.') != 0) { // if a file and not a DOT file
            
$dlist .= "\n" '<li><a href="' htmlentities($file) . '">';
            
$dlist .= htmlentities($file) . '</a></li>';
            
$cntFile += 1// increment the counter
        
}
    }
    if (
$cntFile 1$dlist .= "\n" '<div><small>(none)</small></div>';
    
$dlist .= "\n" '</ul>';
    
closedir($dir); // close the directory
} else $dlist .= "\n" '<p>(Could not read the directory, or the directory does not exist.)</p>';

echo 
'<h2>Directory Contents</h2>';

if (
$cntFile+$cntDir 0) { // items found, any file or directory
    
echo "\n" '<p>';
    if (
$cntFile && $cntDir 0) { // files and directories found
        
echo '<strong>' . ($cntFile $cntDir) . ' item';
        echo ((
$cntFile $cntDir 0) ?'s':'') . ' total:</strong> ';
    }
    if (
$cntDir 0) { // directories found
        
echo $cntDir ' item' . (($cntDir 0)? 's':'') . ' with no file extension';
    }
    if (
$cntFile && $cntDir 0) { // files and directories found
        
echo ', ';
    }
    if (
$cntFile 0) { // files found
        
echo $cntFile ' item' . (($cntFile 0) ?'s':'') . ' with a file extension';
    }
    echo 
'.</p>';
} else echo 
"\n" '<div><small>(directory empty)</small></div>';

echo 
$dlist;
echo 
'<p>
DOT files, or Unix hidden files, are deliberately excluded from this list. 
The directory listing can be omitted, or can be linked by following the 
examples shown here. By expanding these examples it would also be possible
to only display a select list of file extension types, such as PDF or DOC.
</p>'
;

?>


[Updated: Sunday, November 18, 2007]