<%
## php script to list contents of a directory. This file should be named index.php
##
## whether to display subdirectories
$dirs=true;

$count=0;
$dir_name=".";
$handle=opendir($dir_name);
while ($file = readdir($handle))
{
  $count=$count+1;
  $filearray[$count] = $file;
}
closedir($handle);
sort($filearray);
print "<h3>Files</h3>";
for ($count=1;$count<count($filearray);$count++)
{
  $filename=$filearray[$count];
  $size=get_size(filesize($filename));
  if(strpos($filename,".")!==false&&strpos($filename,".")!=0&&substr($filename,-4)!=".php") print "<a href=\"".$filename."\">$filename</a> (".$size.")<br>";
}
if($dirs==true)
{
  print "<h3>Directories</h3>";
  for ($count=1;$count<count($filearray);$count++)
  {
    $filename=$filearray[$count];
## only display subdirectories which have their own index.php file
    if(strpos($filename,".")===false&&file_exists($filename."/index.php")) print "<a href=\"".$filename."/index.php\">$filename</a><br>";
  }
}

## function to return sensibly formatted file size.
function get_size($size) {
  $bytes = array('bytes','KB','MB','GB','TB');
  foreach($bytes as $val) {
    if($size > 1024){
      $size = $size / 1024;
    }else{
      break;
    }
  }
  return round($size, 2)." ".$val;
}
%>