2013-07-06 39 views
0
文件

我的代码遍历目录,并显示所有文件和文件夹,在那里我有我不想显示的index.php文件。如何隐藏READDIR()PHP

<?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>'; 
    } 
?> 

我怎样才能做到这一点?

+0

哪种类型的文件,你不想diaplay? –

+0

我想要隐藏的文件的index.php –

回答

1

入住快速和肮脏的:

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

但我肯定会转移到像FilesystemIterator或​​3210一些更一致的方法。

+0

翻译请它是如何工作 –

+0

@AndreiClaudiu它是如何工作的呢? ^^ – moonwave99

+0

我从你的img标签看到$ cat/image.php,你只需要子目录。所以如果你有一个subdir,你应该检查一下“is_dir”函数。这样你就不会得到index.php。但是moonwave99建议查看glob()可能会更好。参见[这里](http://nl3.php.net/glob)和[这里](http://stackoverflow.com/questions/3713588/how-do-i-exclude-non-folders-files-from-此-READDIR功能?RQ = 1)。 – Rik

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

    // display the file here 
}