Results 1 to 2 of 2

Thread: List Contents of Directory With exception

  1. #1
    Join Date
    Dec 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default List Contents of Directory With exception

    I am trying to display in a list format the contents of a specific directory. However I would like to exclude specific files for example:
    .htaccess
    error_log
    another_file.html
    some_sub_Directory.

    Just having a hard time with the the and if else statements, I am assuming that is the way to go, if not please advise, here is the code I am working with:

    #######################
    <?php

    $path = "/Directory Path/To/Folder";
    $dh = opendir($path);
    $i=1;
    while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != "..") {
    echo "$i. <a target=_blank href='$file'>$file</a><br />";
    $i++;
    }
    }
    closedir($dh);
    ?>
    #########################

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    PHP Code:
    <?

    //define the path as relative
    $path "/home/yoursite/public_html/whatever";

    //using the opendir function
    $dir_handle = @opendir($path) or die("Unable to open $path");

    echo 
    "Directory Listing of $path<br/>";

    //running the while loop
    while ($file readdir($dir_handle)) 
    {
       if(
    $file!="." && $file!=".." && $file!=".htaccess" && $file!="error_log" && $file!="another_file.html")
          echo 
    "<a href='$file'>$file</a><br/>";
    }


    //closing the directory
    closedir($dir_handle);

    ?>
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

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
  •