Skip to content

Instantly share code, notes, and snippets.

@icco
Created February 25, 2009 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icco/70322 to your computer and use it in GitHub Desktop.
Save icco/70322 to your computer and use it in GitHub Desktop.
To list files in a directory in a safe manor
<?php
/**
* A file that lists all files in a directory, ignoring hidden files and itself.
* Also this uses a natural sort and a simple stylesheet.
*/
?>
<html>
<head>
<title>Nat's School Files</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<?php
// open this directory
$myDirectory = opendir(".");
// get each entry
while($entryName = readdir($myDirectory))
{
$dirArray[] = $entryName;
}
// close directory
closedir($myDirectory);
// count elements in array
$indexCount = count($dirArray);
// sort 'em
natcasesort($dirArray);
// Needs to be done since the sort is relative. This reassigns all keys.
$dirArray = array_values($dirArray);
// print 'em
print("<ul class=whitelinks>\n");
// loop through the array of files and print them all
Print ("<li>$indexCount files</li><li>---------------------------------------</li>\n");
for($index=0; $index < $indexCount; $index++)
{
// don't list hidden files
if (substr("$dirArray[$index]", 0, 1) != "." && $dirArray[$index] != "index.php")
{
print("<li class='item'><a href=\"$dirArray[$index]\">$dirArray[$index]</a>");
//print("<div class='type'> - ");
//print(date ("F d Y H:i:s.", filemtime($dirArray[$index])) + "</div>");
print("<div class='size'> - ");
print(filesize($dirArray[$index]));
print("b </div></li>\n");
}
}
print("</ul>\n");
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment