Search code examples
phpreaddir

how to hide a file from readdir() php


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?


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().