我的代码遍历一个目录,并显示我有一个我不想显示的index.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 :(得分:1)
保持快速和肮脏:
if ($cat != '.'&&$cat != '..' && $cat != 'index.php'){ ... }
但我肯定会采用更加一致的方法,例如FilesystemIterator
或glob()
。
答案 1 :(得分:0)
while($cat = readdir($handle)) {
if (in_array($cat, array('.', '..', 'index.php')))
continue;
// display the file here
}