From my library.
Make sure you don't close the directory with a slash.
ex.
full/path/to/directory/ <-wrong
full/path/to/directory <-right
<?php
function dirContents($dir) {
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if(in_array($file,array('.','..'))) { continue; }
if(is_dir($dir.'/'.$file)) {
$contents[$dir.'/'.$file] = dirContents($dir.'/'.$file);
} else {
$contents[] = $dir . '/' . $file;
}
}
closedir($dh);
}
}
return $contents;
}
function processArray($array) {
if(is_array($array)) {
echo '<ol>';
foreach($array as $key => $value) {
if(is_int($key)) {
echo '<li><a href="'.$value.'">'.str_replace('/','',strstr(strrchr($value,'/'),'.',true)).'</a></li>';
}
else {
echo '<li><span style="font-weight:bold">' . $key . '</span>';
processArray($value);
echo '</li>';
}
}
echo '</ol>';
}
}
processArray(dirContents('PATH TO DIRECTORY'));
?>