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
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts

    . and .. from directory

    Here is a code that will read all the files in the directory and output them as links to them but on the page it add's in 2 extra links before outputting the list:
    Code:
    <a href="/schedule/.">.</a><br><a href="/schedule/..">..</a><br>
    Here is the PHP:
    PHP Code:
    $dir "./schedule/";

    if(
    is_dir($dir) && is_readable($dir))
    {
        
    $dh opendir($dir);
        while((
    $file readdir($dh)) !== false)
        {
            echo 
    "<a href=\"/schedule/".$file."\">".$file."</a><br>";
        }
    }
    else
    {
        echo 
    'Invalid directory';


  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    So you want to get rid of them? You can do something like:
    PHP Code:
    while((($file readdir($dh)) !== false) && (strcmp($file'.') != 0) && (strcmp($file'..') != 0)) 
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #3
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts
    It didn't rid of it and I was wondering why . and .. was included in the list?

  • #4
    Regular Coder
    Join Date
    Nov 2006
    Posts
    246
    Thanks
    13
    Thanked 26 Times in 24 Posts
    Not sure why they show, but this will get rid of them:

    PHP Code:
    $dir "./schedule/";

    if(
    is_dir($dir) && is_readable($dir))
    {
        
    $dh opendir($dir);
        while((
    $file readdir($dh)) !== false)
        {
            if(
    $file != '.' && $file != '..'){
                echo 
    "<a href=\"/schedule/".$file."\">".$file."</a><br>";
            }
        }
        
    closedir($dh);
    }
    else
    {
        echo 
    'Invalid directory';

    Also, should close the directory when done (also added that).
    Last edited by Majoracle; 11-18-2007 at 11:32 PM.

  • Users who have thanked Majoracle for this post:

    Dat (11-18-2007)

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    They show up because every directory has a link to itself . and its parent .. in addition to links to all the files it contains. They are needed for the operating system to work when you want to use relative addresses.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    Dat (11-19-2007)


  •  

    Posting Permissions

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