File Manipulation Code

  Home arrow File Manipulation Code arrow Directory Lister
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:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    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

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap