2012-04-06 106 views
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>'; 
?> 
+2

發佈您的代碼。 – m0skit0 2012-04-06 12:05:56

回答

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()readdir()的,你可以只使用SCANDIR這樣的:

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

} 

但它看起來像你想抓住一些媒體文件,並顯示它們。因此,一個更好的解決辦法是使用glob()功能,像這樣:

foreach (glob("*{.mp3,.mp4,.jpg,.png}", GLOB_BRACE) as $filename) { 
    echo "$filename size " . filesize($filename) . "\n"; 
} 
0

循環是否將文件作爲對象返回?那麼你可以添加一個if語句到文件名參數中

2

只要你看到它不需要的東西,就放置一個continue語句,該語句將循環返回到條件。

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>'; 
     } 
    } 
+0

在這種情況下,我認爲使用'for($ index = 1; $ index <$ indexCount; $ index ++)'會更好。 – Timur 2012-04-06 12:13:06

+0

是的,你可以 – amd 2012-04-06 13:39:58

0

與期望比較當前的文件名就忽略了所需的文件。例如,你可以做到這一點在列表檢索:

$myDirectory = opendir('.'); 

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

closedir($myDirectory); 

不要依賴指數(號文件),因爲你可能index.php不首先在列表中。

0
for($index = 0; $index < $indexCount; $index++) { 
if(index == 0) continue; 
// other stuff 
}