Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2006
    Location
    Idaho
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Directory Listing Control

    I hope this is fairly simple. I want to create a directory where I can drop stuff that people can download [for free]. I have a great php function that reads the directory and writes out a contents list with live links. One problem, it also writes the single and double dots [. and ..] that lets people move out of the current directory list. This I need to eliminate. I only want the list of actual contents without the change directory links.

    [?php
    $d = dir('freebies');
    echo "Directory: " . $d->path . "<br>";
    while (false !== ($entry = $d->read())) {
    echo "<a href='/freebies/".$entry."'>".$entry."</a><br>";
    }
    $d->close();
    ?]
    .
    + Christ is the Rock - Rock On!

  • #2
    New Coder
    Join Date
    Aug 2006
    Location
    Idaho
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Got the answer from another coder.

    sliver ROCKS!

    [?php
    $d = dir('freebies');
    echo "Directory: " . $d->path . "<br>";
    while (false !== ($entry = $d->read())) {
    if($entry != "." && $entry != "..") { //Don't let . and .. through
    echo "<a href='/freebies/".$entry."'>".$entry."</a><br>";
    }
    }
    $d->close();
    ?]
    Last edited by Scriptbanger; 12-22-2006 at 07:06 PM. Reason: had an error
    .
    + Christ is the Rock - Rock On!

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You actually colored it by yourself?
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    LOL! Now THAT's dedication!
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)

  • #5
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Wow!

    That is a fine solution, but could get really tedious if you wanted to block certain folders along with the '.' and '..'.

    If you did this:
    PHP Code:
    <ul>
    <?php
    $skip_folders 
    = array('.','..','other','private_folder');

    $dir "PATH TO DIR YOU WANT TO READ"
       
    if (is_dir($dir))
        {
         if (
    $dh opendir($dir))
          {
           while ((
    $file readdir($dh)) !== false)
           {
           if(!
    in_array($file$skip_folders))
                    {
                        print(
    "<li><a href=\"$file\">$file</a></li>");
                    }
                }
            
           }
          
    closedir($dh);
         }
    ?>
    </ul>
    you would be able to filter out any folders or files which you didn't want to show up simply by adding them to the array.

    Dan
    Last edited by whizard; 12-22-2006 at 09:01 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

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