简体   繁体   中英

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?

Staying quick and dirty:

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

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

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

  // display the file here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM