0

I am trying to display the files in my folder, "Amir", but my table keeps displaying all of the files, "." and "..". How can I prevent my table from displaying ".." or "."? I have placed the code and a picture below.

<?php

echo '<table border="1">
<tr>
    <th>Name</th>
  </tr>';

if ($handle = opendir("amir")) {
    //echo "Directory handle: $handle\n";
    //echo "Entries:\n";


    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
    //    echo "$entry\n";

        echo'



  <tr>
    <td><a href="'. $entry . '">'. $entry. '</a></td>
  </tr>
';
    }

    /* This is the WRONG way to loop over the directory. */
    while ($entry = readdir($handle)) {
        //echo "$entry\n";
    }

    closedir($handle);
}

echo'</table>
';
?>

Image of my table

1

Edit: Try this. (slightly simplified from [here])

<?php
// open this directory 
$myDirectory = opendir("./");

// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//count num files
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

//sort by name
sort($dirArray);

//print files
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><th>Filename</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
        print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
        print("<td>");
        print(filesize($dirArray[$index]));
        print("</td>");
        print("</TR>\n");
    }
}
print("</TABLE>\n");
?>
| improve this answer | |
  • where in the code would I put this? – Coder404 Aug 10 '12 at 20:36
  • @Coder404 - See my revision. The key code is if(substr("$dirArray[$index]", 0, 1) != ".") which means that if the file name contains at least one dot in the first position, don't show. – ionFish Aug 10 '12 at 20:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.