PHP: List Contents of a Directory


SUBMITTED BY: Guest

DATE: Dec. 11, 2014, 3:14 p.m.

FORMAT: PHP

SIZE: 1.1 kB

HITS: 842

  1. // open this directory
  2. $myDirectory = opendir(".");
  3. // get each entry
  4. while($entryName = readdir($myDirectory)) {
  5. $dirArray[] = $entryName;
  6. }
  7. // close directory
  8. closedir($myDirectory);
  9. // count elements in array
  10. $indexCount = count($dirArray);
  11. Print ("$indexCount files<br>\n");
  12. // sort 'em
  13. sort($dirArray);
  14. // print 'em
  15. print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
  16. print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
  17. // loop through the array of files and print them all
  18. for($index=0; $index < $indexCount; $index++) {
  19. if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
  20. print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
  21. print("<td>");
  22. print(filetype($dirArray[$index]));
  23. print("</td>");
  24. print("<td>");
  25. print(filesize($dirArray[$index]));
  26. print("</td>");
  27. print("</TR>\n");
  28. }
  29. }
  30. print("</TABLE>\n");

comments powered by Disqus