0

特定のディレクトリ内のすべてのファイルをリストする FOR ループがあります。結果の 1 つにも INDEX.PHP がありますが、これは必要ありません。ループの最初の結果/エントリです...

この最初の結果をスキップして、2 番目の結果からループを開始する方法はありますか? 助けてください。

    <?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

私はそれを行う2つの方法を見ます:

  • 後で 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
    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

ループはファイルをオブジェクトとして返しますか? 次に、if ステートメントを filename パラメータに追加するだけです。

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