Ever worried about listing the content of a directory, but your hosting provider does not allow directory listing?
This little PHP application will help you. It lists the content of the directory where it resides and it is fully configurable.
- You can display hidden files (or better not ;-).
- Show / don't show executables.
- Show / don't show directories.
- Show / don't show uplinks.
Decide if you want to
- generally show all and exclude files by extension.
- generally show no files and include files by extension.
In contrast to the directory listing of the Apache wenserver, phpDIRList 2.0 generates valid XHTML 1.0 Strict code and valid CSS 2.1
phpDIRList 2.0 is released under BSD style license. Feel free to use and modify it als long as you leave the copiright notice in the PHP source.
<?php
/*
phpDIRList 2.0 - (c)2005 Ulrich S. Kapp :: Systemberatung :: web-programmierung
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of the Ulrich S. Kapp nor the names of its contributors may be
used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Page title, please edit only the string before '.str_replace ...'.
$title = "Index of ".str_replace (substr (strrchr ($_SERVER["SCRIPT_NAME"] , "/"), 1), "", $_SERVER["SCRIPT_NAME"]);
$search_type = "exclude"; // include or exclude
/* If you configure '$search_type' to include, then phpDIRList 2.0 will
only show the files configured in the '$valid_files' array.
Caution: never change the first element of this array. */
$valid_files = array ("\\/", "pdf", "jpeg", "gif");
/* If you configure '$search_type' to exclude, then phpDIRList 2.0 will show
ALL files except the files configured in the '$invalid_files' array.
Caution: never change the first element of this array. */
$invalid_files = array ("\\/", "inc", "conf");
/* '$show_hidden' decides if phpDIRList 2.0 shows hidden files.
Caution: if set to TRUE, phpDIRList 2.0 will show ALL secret files like
a .htaccess or .htpasswd file, if there is one in the folder. */
$show_hidden = FALSE; // TRUE or FALSE
// '$show_folders' tells phpDIRList 2.0 to show subdirectories.
$show_folders = TRUE; // TRUE or FALSE
// '$show_uplink' makes phpDIRList 2.0 to display the directory uplink.
$show_uplink = TRUE; // TRUE or FALSE
// '$show_executables' tells phpDIRList 2.0 to display executables.
$show_executables = FALSE; // TRUE or FALSE
// -- End Configuration -- //
function test_ext ($file) { // This is the function to check the validity of a directory entry.
// Global all necessary variables
global $valid_files;
global $invalid_files;
global $search_type;
global $show_hidden;
global $show_folders;
global $show_uplink;
global $show_executables;
// Check '$search_type' and set initial value for '$create_entry'.
if ($search_type == "exclude") {
$create_entry = TRUE;
} else $create_entry = FALSE;
// Check if show_folder is set or else it is not a folder
if (!$show_folders AND is_dir( $file)) {
$create_entry = FALSE;
if ($show_uplink AND preg_match("/^[\.]{1,2}$/", $file)) $create_entry = TRUE;
} else if ($show_folders AND is_dir( $file)) {
$create_entry = TRUE;
if (!$show_uplink AND preg_match("/^[\.]{1,2}$/", $file)) $create_entry = FALSE;
}
// Check if show_folder is set or else it is not a folder
if ($show_executables AND is_file($file) AND is_executable( $file)) {
$create_entry = TRUE;
} else if (!$show_executables AND is_file($file) AND is_executable( $file)) {
$create_entry = FALSE;
}
// Check for valid or invalid file types.
if ($search_type == "include") {
if ($ext = substr (strrchr ($file, "."), 1)) {
if (array_search ($ext, $valid_files)) $create_entry = TRUE;
}
} else if ($search_type == "exclude") {
if ($ext = substr (strrchr ($file, "."), 1)) {
if (array_search ($ext, $invalid_files)) $create_entry = FALSE;
}
}
// At last check if show_hidden is set or else it is not a hidden file
if (!$show_hidden AND preg_match("/^\./", $file)) {
$create_entry = FALSE;
if ($show_uplink AND preg_match("/^[\.]{1,2}$/", $file)) $create_entry = TRUE;
} else if ($show_hidden AND preg_match("/^\./", $file)) {
$create_entry = TRUE;
if (!$show_uplink AND preg_match("/^[\.]{1,2}$/", $file)) $create_entry = FALSE;
}
// Finally return if an entry should be created or not.
return ($create_entry);
}