Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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

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.

1 Answer 1

up vote 0 down vote accepted

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");
?>
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.