filtering folders and files

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
User avatar
swirlee
Moderator
Moderator
Posts: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Thu Jul 10, 2003 12:53 pm

The function to tell if a file is a directory is is_dir(). Use it well. Also, when you're doing simple matching, ereg functions are massive overkill, and if you're just iterating through an array, use foreach(). Anyway, I made your code work and also cleaned it up a ton. I recommend you read some articles on conventions and code readability in PHP programming.

Code: Select all

<?php 
   $dp = opendir(".");
   
   while($file = readdir($dp)) {
      $filenames[] = $file;
   }
   closedir($dp);
   
   foreach($filenames as $filename) {
      if(   is_dir($filename) &&
            $filename != '..' &&
            $filename != '.'
      ) {
         echo '<a href="' . $filename . '">' . $filename . "</a><br>\n";
      }
   }
?>
Image

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests