0

My code loops through a directory and displays all the files and folders where I have a index.php file that I don't want to be displayed.

<?php 

    $directory = 'jocuri';
    $row = 0;

    if ($handle = opendir($directory.'/')) 
    {
        echo '<table border="1">';    
        while ($cat = readdir($handle)) 
        {
            if ($cat != '.' && $cat != '..') 
            {
                if($row==0) echo '<tr>';

                echo '<td align="center">';
                echo '  <a href="'.$directory.'/'.$cat.'" style="text-decoration:none">';
                echo '    <img src="'.$directory.'/'.$cat.'/image.php" style="display:block" />'.str_replace('_', ' ', $cat);
                echo '  </a>';
                echo '</td>';

                if($row == 2) 
                {
                  echo '</tr>';
                  $row = -1;
                }
                $row++;
            }
        }
        echo '</table>';
    }
?>

How can i achieve that?

msturdy
  • 10,479
  • 11
  • 41
  • 52

2 Answers2

1

Staying quick and dirty:

if ($cat != '.'&&$cat != '..' && $cat != 'index.php'){ ... }

But I'll definitely move to some more consistent method like FilesystemIterator or glob().

moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • I see from your img tag $cat/image.php you only want sub-directories. So you should check with an "is_dir" function if you've got a subdir. This way you wouldn't get index.php. But moonwave99's suggestion to look in glob() is probably better. See [here](http://nl3.php.net/glob) and [here](http://stackoverflow.com/questions/3713588/how-do-i-exclude-non-folders-files-from-this-readdir-function?rq=1). – Rik Jul 06 '13 at 13:01
0
while($cat = readdir($handle)) {
  if (in_array($cat, array('.', '..', 'index.php')))
    continue;

  // display the file here
}
nice ass
  • 16,471
  • 7
  • 50
  • 89