Results 1 to 6 of 6

Thread: PHP Directory Lister

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Directory Lister

    I have been wanting to make a php directory lister for some time now, but can't seem to get it right. I have very limited knowledge of how php handles this type of thing, so I have been forced to use tutorials. I just went through this one and got this code:
    PHP Code:
    <?php

    // If dir is set in url
    if(isset($_GET['dir']) && is_dir($_GET['dir'])) {
        
    $dir $_GET['dir'];
    } else {
        
    $dir "./";
    }

    // Files to exclude
    $exclude[] = "index.php";

    // If file is not in exclude array, display it
    if($handle == opendir($dir)) {
        while(
    $fp == readdir($handle)) {
            if(!
    in_array($fp$exclude)) {
                echo 
    "$fp<br /><br />";
            }
        }
    }

    ?>
    In my limited understanding of this, it really seems like something is missing and sure enough when I run the page, i get a blank window. Can anyone help me out with this? Thanks in advance
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try the following:

    Code:
    <?php
    
    // If dir is set in url
    if(isset($_GET['dir']) && is_dir($_GET['dir'])) {
        $dir = $_GET['dir'];
    } else {
        $dir = './';
    }
    
    
    
    // Files to exclude
    $exclude[] = "index.php";
    
    
    // If file is not in exclude array, display it
    
    if (is_dir($dir)) {
    
       if ($handle = opendir($dir)) {
           while (($fp = readdir($handle)) !== false) {
    
              if ($fp != '.' && $fp != '..') {
                 if (!in_array($fp, $exclude)) {
                    echo $fp.' <BR>';
                 }
              }
           }
           closedir($handle);
       }
    }
    ?>
    Notice the part in red, this is what I added to make it list the files in the directory. Also, the part in blue is what I added to make it to where it does not show ".." or "." in the file list. Take that part out if you wish.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Testing, works nicely. As a build on question, is there a somewhat simple way of having links to view the files and open the folders? Also, how might I go about giving certain file types an image (like a directory gets a folder, etc). Perhaps through the use of eregi(".php", $fp) or something like that? Thanks
    Thanks DD, you saved me countless times

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    I had written a "File Manager" type script (sort of like what you are talking about). As soon as I find it, I'll post it.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    As promised, I have attached a zip file with an imgs folder and index.php. index.php is the on page file manager (you will have to modify the script a little to suit your needs). The imgs folder contains 2 images (an image for directories and one for files).

    Hope this helps, and let me know if you need any more help.
    Last edited by thetestingsite; 03-10-2007 at 08:46 PM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks testing, I learned a lot from that script
    Thanks DD, you saved me countless times

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •