filtering folders and files

General discussions related to php

Moderators: egami, macek, gesf

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

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

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

Post Reply