Results 1 to 3 of 3

Thread: Directory Listing

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    32

    Arrow Directory Listing

    Can anybody tell me how to list the contents of a directory??
    Not all files but the files of a specified file type.
    I ve managed to display all files using readdir, but still dont know how to filter files of a particular type.
    Hope you guys can help me
    Thanx in adwance

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Directory Listing

    this will grab any files in $dir with a filetype listed in $filetypes. make sure you add a "|" to the end of each filetype, including the last. wasn't sure if you were only looking to display one specific filetype, or multiple, so i did it this way.

    PHP Code:
    <pre>
    <?
      //the directory to look in
      $dir = "./";
      
      //this is a list of the valid filetypes, seperated by a "|"
      //make sure the last item also has a "|" at the end!
      $filetypes = "html|zip|";

      //start reading the directory
      if(is_dir($dir)){
        if($opendir = opendir($dir)){
          while(($file = readdir($opendir)) !== false){

            //find the last occurence of a period in the filename
            $typepos = strrpos($file, ".");

            //check if the typepos was returned
            // --> if not, it does not have a filetype, so we ignore it
            if($typepos > 0 && $file != ".."){

              //isolate the filetype
              $type = substr($file, $typepos + 1, strlen($file) - $typepos);

              //check if the filetype is something we want
              if(strpos($filetypes, $type . "|") !== false)
                echo $file . " - " . strtoupper($type) . " file\n";
            }
          }
          closedir($opendir);
        }
      }
    ?>
    </pre>
    hope it helps you out.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    32

    Exclamation Re: Directory Listing

    Thanx for the reply!!!
    I was wondering whether there is a built in function to do so; just like dir() or so which accepts wild charecters to filter.

Posting Permissions

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



Click Here to Expand Forum to Full Width