WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : List the content of the directory of your webserver where this small PHP Script resides.
Categories : PHP, Filesystem, Directories, CSS Click here to Update Your Picture
Ulrich Kapp
Date : Apr 01st 2005
Grade : 5 of 5 (graded 5 times)
Viewed : 12580
File : 4124.php
Images : No Images for this code example.
Search : More code by Ulrich Kapp
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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.
*/


// -- Configuration -- //

// Table heading values
$file_name = "Filename";
$file_size = "Size in Bytes";
$file_date = "Last Modified";

// 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);
}

// Try to get a directory handle and continue if it returns TRUE.
if ($dir = @opendir("./")) {
   
$output = ""; // Empty the HTML-output variable.
   
while (($file = readdir($dir)) !== FALSE) { // Read the directory step by step and process the data.
       
if (test_ext ($file)) { // Check if the entry should be displayed or not and continue if TRUE.
           
$stats = @stat ( "./".$file); // Get the file stats.
            // And append a table row to the output variable.
           
$output .= '
        <tr class="line">
            <td class="col1">
                <h2><a title="'
.$file.'" href="'.$file.'">'.$file.'</a></h2>
            </td>
            <td class="col2">'
.number_format ($stats[7], 0, ",", ".").'</td>
            <td class="col3">'
.str_replace (" ", " · ", date ("d.m.Y H:i", $stats[9])).'</td>
        </tr>'
."\n";
        }
    } 
   
closedir($dir); // Close the directory handle.
} else {
   
$output = "<tr><td colspan=\"3\"><h1>Cannot open Directory!</h1></td></tr>"; // Display error message if there was no directory handle returned in the leading condition.
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <title><?php echo $title ?></title>
        <style type="text/css" media="screen">
    /* <![CDATA[ */
    h1 { font-family: Myriad Pro, Verdana, Geneva, Helvetica, Arial, Sans-Serif; font-size: small; padding: 0; margin: 0 }
    h2 { font-family: Myriad Pro, Verdana, Geneva, Helvetica, Arial, Sans-Serif; font-size: x-small; padding: 0; margin: 0 }
    p, a, div, td  { font-family: Myriad Pro, Verdana, Geneva, Helvetica, Arial, Sans-Serif; font-size: x-small }
    th {font-family: Myriad Pro, Verdana, Geneva, Helvetica, Arial, Sans-Serif; font-size: small; text-align: left }
    caption { font-family: Myriad Pro, Verdana, Geneva, Helvetica, Arial, Sans-Serif; font-size: medium; font-weight: bold; text-align: left; min-width: 28em; margin-bottom: 1em }
    address  { font-family: Myriad Pro, Verdana, Geneva, Helvetica, Arial, Sans-Serif; font-size: x-small }
    hr { width: 100%; height: 1px }
    .dirlist { min-width: 28em }
    .col1 { width: 20em; min-width: 14em; font-weight: bold; padding-right: 1em }
    .col2 { width: 10em; min-width: 7em; text-align: right; font-weight: bold; padding-right: 2em }
    .col3 { width: 10em; min-width: 7em; font-weight: bold }
    .subline { font-size: xx-small }
    .subline a { font-size: xx-small }
/* ]]> */
        </style>
    </head>
    <body>
        <table class="dirlist">
            <caption><?php echo $title; ?></caption>
            <tr>
                <th class="col1"><h1><?php echo $file_name; ?></h1></th>
                <th class="col2"><?php echo $file_size; ?></th>
                <th class="col3"><?php echo $file_date; ?></th>
            </tr>
            <tr><td colspan="3"><hr /></td></tr>
            <?php echo $output; ?>
            <tr><td colspan="3"><hr /></td></tr>
        </table>
        <?php echo str_replace ("ADDRESS", "address", $_SERVER["SERVER_SIGNATURE"]); ?>
        <p class="subline">phpDIRList 2.0  -  © 2005 <a title="ulrich s. kapp :: systemberatung :: web-programmierung" href="http://www.ulrich-kapp.de/">ulrich s. kapp :: systemberatung :: web-programmierung</a>  -  <a href="http://validator.w3.org/check?uri=referer">W3C ⇒ XHTML 1.0 Strict</a>  -  <a href="http://jigsaw.w3.org/css-validator/check/referer">W3C ⇒ CSS</a></p>
    </body>
</html>



a file explorer for the web, filesystem php php3 files dirs directories pictures files windows linux system list ls scripts
Categories : PHP, URLs, Directories, Filesystem
grab directory listings into an array the example prints out each subdirectory in the main dir - further work is to be performed on this one
Categories : Filesystem, PHP, Directories, Search, Utilities
Directory TreeView - File Manager & Explorer - FTP - Utility - PHP/HTML -
Categories : PHP, Directories, FTP, Filesystem, HTML and PHP
getDirArray(Path,Filter,Sorted): Returns an array of the files in a directory, filtered by regular expression and either sorted or randomized. Good for random pictures and graphics.
Categories : PHP, Filesystem, Directories
Listing the 10 most recently updated files in a given dir by using last- modified variable and printing to html with link to the file
Categories : PHP, Directories, Filesystem
A function which places the path and name of all subdirectories into an array
Categories : PHP, Filesystem, Arrays, Directories
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP
Directory viewer, customize how you display the file structure, easy to understand. Found out about PHP 3 days ago, and this is my first prog.
Categories : HTML and PHP, Complete Programs, Directories, Filesystem, PHP
Extended Get File List Function
Categories : PHP, Filesystem, Search, Directories
Directory Viewer, Directory Content Viewer, Directory Structure to HTML. This code will basically create a complete set of HTMLs to let a user navigate through any directory you want. Excellent code for large file sharing pages.
Categories : Directories, Filesystem, PHP
Using PHP to Delete a directory with all sub directories and files using FTP
Categories : PHP, FTP, Directories, Filesystem
PHP4 DirectoryIterator Class
Categories : PHP, PHP Classes, Filesystem, Directories
Open directory and File download
Categories : PHP, Filesystem, Directories, HTML and PHP
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
An efficient iterative and buffered text file reader
Categories : PHP, Classes and Objects, Filesystem, PHP Classes, Log Files
 Olaf Lederer wrote :1303
works not for me on my php 4 windows test location:

:-(

is_executable() became available with Windows™ in PHP version 5.0.0.