Question

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?

Was it helpful?

Solution

Staying quick and dirty:

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

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

OTHER TIPS

while($cat = readdir($handle)) {
  if (in_array($cat, array('.', '..', 'index.php')))
    continue;

  // display the file here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top