I have a a FOR loop that lists all files within a certain directory. One of the results is also INDEX.PHP, a result which I don’t need. It’s the first result/entry of the loop…
Is there any way to skip this first result and start looping from the second one?
Please help.
<?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>';
?>
I see two ways of doing it:
Instead of
for($index = 0; $index < $indexCount; $index++) { ...},do
for($index = 1; $index < $indexCount; $index++) { ...}for example:
But they are a few ways you can improve your code. Instead of using
opendir()andreaddir(), you can just use scandir like this:But it looks like you want to grab some media file and display them. So an even better solution would be to use the glob() function, like this: