File Manipulation Code
  Home arrow File Manipulation Code arrow Directory Lister
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FILE MANIPULATION CODE

Directory Lister
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2005-07-23

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A fairly simple script to list all directories and files in a base directory. Then allows users to navigate through the directories and sub-directories.

    You can find more scripts on my site here

    Featuring a fairly simple permission mode, you can set if the user can navigate above a given directory. You can also disable the whole script by changing $allow_access to 0.

    Note: Requires 2 images of your choice (A directory image and file image)

    By : andrew

    <?
    /*

    Author: Andrew Walsh
    Date: 23/07/2005
    Cw-Username: Andrew

    */

    //Config section
    $base_path = "/path/to/directory/"; //Set the base directory
    $above_base = 0; //Can the user navigate above the base directory or not?
    $directory_img = "./dir.gif";
    $file_img = "./file.gif";
    $allow_access = 1; //If false the script will not run...

    if($allow_access == 1){

    if(!isset($_REQUEST['path'])){ //If no path set create a base path
    $path = substr($base_path, 0, -1);
    }else{
    $path = $_REQUEST['path']; //If path is set in url then use that path
    }

    //Is the user trying to navigate above the base directory?

    if($above_base == 1){ //Is this option enabled?

    $b = explode("/", $base_path); //Split up the base path
    $c = count($b); //Count elements in the base path
    $c--; //Subtract last section of base path

    //Construct the path for the directory above the base directory/path for comparison later
    for($i=0; $i<$c; $i++){
    $r = $b[$i].'/';
    }

    //Is the request path above the base path? is the base_path not in the requested path?

    if($path == $r || strstr($path, $base_path) === FALSE){

    //If yes then stop the script and output error message

    die("You are not allowed to navigate above the base path!"); //Output error

    }else{

    $dir_handle = opendir($path) or die("Unable to open $path"); //Open the path
    }

    }else{
    $dir_handle = opendir($path) or die("Unable to open $path"); //Open the path

    }

    //Generating the path to the directory above the current path....

    $p = explode("/", $path); //Breakup the path into parts
    $c = count($p); //Count parts in the path
    $c--; //Subtract the ending section

    //Generate the path
    for($i=0; $i<$c; $i++){
    $up_path .= $p[$i].'/';
    }
    $up_path = substr($up_path, 0, -1); //Trim off the ending /

    //Display links to current path and directory above current directory
    echo '
    <a href="?path='.$path.'"><b>.</b></a><br>
    <a href="?path='.$up_path.'"><b>..</b></a><br>
    ';

    while($f = readdir($dir_handle)){ //Create arrays of directories and files

    if(is_dir($f)){ //If file being read is a directory then store in the directory array

    $d[] = $f; //Add the link to the directories array

    }else{

    $fs[] = $f; //Else its a file

    }

    }

    if(count($d) < 1){

    }else{

    sort($d); //Sort the directories array

    }

    if(count($fs) < 1){

    }else{

    sort($fs); //Sort the file array

    }

    foreach($d as $dir){ //Loop through directory array

    if($dir != "." && $dir != ".."){ //Remove the . and .. links as these are generated by the script

    //Echo out the directories with the directory image next to them....
    echo '<a href="?path='.$path.'/'.$dir.'"><img src="'.$directory_img.'" border="0" /><b>'.$dir.'</b></a><br>';
    }

    }

    foreach($fs as $f){ //Loop through the files array

    //Display the links to the files with the file image before....
    echo '<img src="'.$file_img.'" border="0" />'.$f.'<br>';

    }

    closedir($dir_handle); //Close the current working directory....

    }else{

    die("You are not allowed to access this script!");

    }
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More File Manipulation Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    Build Forge Express demo: Enabling software delivery excellence for small and midsized businesses

    This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process.
    FREE! Go There Now!


    NEW! Don't wait! Try the Rational Application Developer (RAD) v7.5 open beta code today

    Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems.
    FREE! Go There Now!


    NEW! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! Project and Portfolio Management Executive Resource Kit

    Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle.
    FREE! Go There Now!


    NEW! Run your first CICS application on a PC using TXSeries for Windows

    Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Method Composer V7.2

    Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996.
    FREE! Go There Now!


    NEW! Using Rational Business Developer to enhance your developer productivity

    Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components.
    FREE! Go There Now!


    NEW! Webcast: Application security testing and Web compliance

    Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development.
    FREE! Go There Now!


    NEW! Webcast: IBM Rational Build Forge - Beyond the Build

    The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size.
    FREE! Go There Now!


    NEW! Whitepaper: Achieving consistency between business process models and operational guides

    Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    FILE MANIPULATION CODE ARTICLES

    - Bandwidth Control with pure PHP
    - Eazy Gallery
    - file_get_contents for PHP < 4.3.0
    - PHP Class: Image Snapshot 1.3
    - Universal downloader
    - Image Gallery v2.0
    - Free/Used Disk Space
    - Directory Lister
    - Directory image view, with selective hidden
    - Move or Copy a Directory (and files and sub ...
    - Ensure_Sub_Directory_Exists
    - Wedit
    - Form Examples Text Boxes to Drop Downs
    - myFiles
    - List files in a directory, no subdirectories





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT