<?
/*
* This sample code is a function that recursively read the contents of a directory and subdirectories. The directory is highlighted with an H2 tag
*/
function read_dir($dir) {
$array = array();
$array[]='<h2>'.$dir.'</h2>';
$d = dir($dir);
while (false !== ($entry = $d->read())) {
$status='';
if($entry!='.' && $entry!='..') {
$entry = $dir.'/'.$entry;
if(is_dir($entry)) {
$array = array_merge($array, read_dir($entry));
} else {
$array[] = $entry;
}
}
}
$d->close();
return $array;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Recursively reading the content of a directory and subdirectories in PHP</title>
</head>
<body style="font-family:tahoma; font-size:11px; color:#333;">
<pre>
<?
print_r(read_dir(dirname($_SERVER['SCRIPT_FILENAME'])));
?>
</pre>
</body>
</html>
|