Listing all Files in a Directory Recursively
<?php
function list_dir($dir) {
//First attempt to open the directory.
try {
if ($adir = opendir ($dir)){
//Scan through the directory.
while (false !== ($item = readdir ($adir))){
//Do not count the . or .. in a directory.
if ($item != "." && $item != ".."){
//Now, if it is another directory,
//then we indent a bit and go recursive.
if (is_dir ($dir . "/" . $item)){
?><span style="font-weight: bold;">
<?php echo $item; ?></span>
<?php
?>
<div style="margin-left: 10px;">
<?php recurdir ($dir . "/" . $item );?>
</div>
<?php
} elseif (is_file ($dir . "/" . $item)){
//Then echo the file.
echo $item . "<br />";
}
}
}
} else {
throw new exception ("Directory could not be openend.");
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Run the function.
list_dir ("test");
?>