Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


echo '<table border="1">

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";


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

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



Image of my table

share|improve this question

closed as off topic by Su', John Conde Aug 10 '12 at 21:50

Questions on Webmasters Stack Exchange are expected to relate to webmastering within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 1 down vote accepted

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

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

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

// close directory

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

//sort by name

//print files
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\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>");
share|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
THANKS. I marked your answer correct – Coder404 Aug 10 '12 at 22:31

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