0

我有一个 FOR 循环,它列出了某个目录中的所有文件。其中一个结果也是 INDEX.PHP,这是我不需要的结果。这是循环的第一个结果/条目......

有没有办法跳过第一个结果并从第二个开始循环?请帮忙。

    <?php
$myDirectory = opendir('.');

while($entryName = readdir($myDirectory)) 
{
    $dirArray[] = $entryName;
}

closedir($myDirectory);

$indexCount = count($dirArray);
echo '<h5>There are ' . $indexCount . ' files in the Media Center</h5>';

sort($dirArray);

echo '<table width="100%">';
echo '<tr>';
echo '<th width="33%" align="center" class="admin_th" style="border-radius: 10px 0 0 0">Download</th>';
echo '<th width="33%" align="center" class="admin_th">Filetype</th>';
echo '<th width="33%" align="center" class="admin_th" style="border-radius: 0 10px 0 0">Filesize (in bytes)</th>';
echo '</tr>';

for($index = 0; $index < $indexCount; $index++) 
{
    if (substr("$dirArray[$index]", 0, 1) != ".")
    {
        echo '<tr><td width="33%" align="center" class="admin_td-even"><a href="' . $dirArray[$index] . '">' . $dirArray[$index] . '</a></td>';
        echo '<td width="33%" align="center" class="admin_td-odd">';
        echo strtoupper(substr($dirArray[$index], -3));
        echo '</td>';
        echo '<td width="33%" align="center" class="admin_td-even">';
        echo filesize($dirArray[$index]);
        echo '</td>';
        echo '</tr>';
    }
}
echo '</table>';
?>
4

6 回答 6

2

一旦你看到你不想要的,放置一个continue支持循环到 for 条件的语句。

于 2012-04-06T12:06:26.667 回答
1

我看到了两种方法:

  • 您可以稍后启动一个索引:

而不是for($index = 0; $index < $indexCount; $index++) { ...},做

for($index = 1; $index < $indexCount; $index++) { ...}

  • 您还可以添加条件:

例如:

for ($index = 0; $index < $indexCount; $index++) {
  if ($dirArray[$index] == 'INDEX.PHP') continue;
  // rest of the loop
}

但它们是改进代码的几种方法。而不是使用opendir()and readdir(),您可以像这样使用 scandir :

foreach (scandir('.') as $file) {

}

但看起来您想获取一些媒体文件并显示它们。因此,更好的解决方案是使用glob()函数,如下所示:

foreach (glob("*{.mp3,.mp4,.jpg,.png}", GLOB_BRACE) as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
于 2012-04-06T12:10:11.920 回答
0

循环是否将文件作为对象返回?那么你可以在文件名参数中添加一个 if 语句

于 2012-04-06T12:06:21.737 回答
0
    for($index = 0; $index < $indexCount; $index++) 
    {
        //skip first entry
        if($index == 0) continue;
        if (substr("$dirArray[$index]", 0, 1) != ".")
        {
            echo '<tr><td width="33%" align="center" class="admin_td-even"><a href="' . $dirArray[$index] . '">' . $dirArray[$index] . '</a></td>';
            echo '<td width="33%" align="center" class="admin_td-odd">';
            echo strtoupper(substr($dirArray[$index], -3));
            echo '</td>';
            echo '<td width="33%" align="center" class="admin_td-even">';
            echo filesize($dirArray[$index]);
            echo '</td>';
            echo '</tr>';
        }
    }
于 2012-04-06T12:11:19.593 回答
0

只需将当前文件的名称与所需文件进行比较,即可忽略所需文件。例如,您可以在列表检索中执行此操作:

$myDirectory = opendir('.');

while( false!==($entryName = readdir($myDirectory)) ) 
{
    if( strtolower($entryName)=='index.php' )continue;
    $dirArray[] = $entryName;
}

closedir($myDirectory);

不要依赖索引(文件数),因为您index.php可能不是列表中的第一个。

于 2012-04-06T12:12:21.070 回答
0
for($index = 0; $index < $indexCount; $index++) {
 if(index == 0) continue;
 // other stuff
}
于 2012-04-06T12:16:24.447 回答