SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a way to get readdir to list only the files in the directory

    but not other directories?

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    UK
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The PHP manual might be able to give you some clues - check out the 2nd example on how to strip out the . and .. entries (which I believe are the folders):

    http://uk2.php.net/readdir

    HTH

    Rob.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Madison, WI
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like this:
    PHP Code:
    $dir='/path/to/dir/you/want/to/read';
    if (
    $handle=opendir($dir)) {
       echo 
    '<pre>';
       while(
    false!==($file=readdir($handle))) {
          if (
    $file!='.' && $file!='..' && !is_dir($file)) {
             echo 
    "$file\n";
          } 
       }
       
    closedir($handle);
       echo 
    '</pre>';
    }
    else {
       echo 
    'There was an error while opening the directory.';

    the if ($file!='.' && $file!='..' && !is_dir($file)) check removes the current and parent directory references (. and .. respectively) and anything else that may happen to be a directory

    P.S. - ed's version is soooo much cooler than mine...use his
    Last edited by jinkas; Nov 11, 2004 at 11:36. Reason: clarify a bit more

  4. #4
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
     
     
    // change to <br> if you're doing html or \n if you're on *nix
     
    $NEWLINE "\r\n";
     
     
    // no trailing slash
     
    $dirPath 'c:/path/to/dir';
     
     
    $dirHandle opendir($dirPath);
     
     if (!
    $dirHandle)
     {   die(
    "Couldn't open directory: $dirPath");
     }
     
     while (
    false !== ($file readdir($dirHandle)) )
     {
         if (
    is_dir($dirPath.'/'.$file) )
         {    continue;
         }
         
         echo 
    "$file $NEWLINE";
        
     }
     
     
    closedir($dirHandle); 
    --ed


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
  •