[code language=”php”]
function readpath($dir,$level,$last,&$dirs,&$files){
//print $dir." (DIR)\n";
$dp=opendir($dir);
while (false!=($file=readdir($dp)) && $level == $last){
if ($file!="." && $file!="..")
{
if (is_dir($dir."/".$file))
{
readpath($dir."/".$file,$level+1,$last,$dirs,$files); // uses recursion
$dirs[] = "$dir/$file"; // reads the dir into an array
}
else{
$files[] = "$dir/$file"; // reads the file into an array
}
}
}
}
/* From PHP.NET */
function get_size($path)
{
if(!is_dir($path))return filesize($path);
$dir = opendir($path);
while($file = readdir($dir))
{
if(is_file($path."/".$file))$size+=filesize($path."/".$file);
if(is_dir($path."/".$file) && $file!="." && $file !="..")$size +=get_size($path."/".$file);
}
return $size;
}
if(isset($_GET[‘i’])){
$start_dir = $_GET[‘i’]; // this fetches the i or directory name through a link specified via the url.
}
else{ // else if no name is specified by link, then use the default
$start_dir = "./parent"; // . means the current directory or whatever name you specify
}
$level=1; // level is the first level started at
$last=1; //this is set the same as level so the script does not read all directories, and only one at a time
$dirs = array(); // SET dirs as an ARRAY so it can be read
$files = array(); //SET files as an ARRAY so it can be read
readpath($start_dir,$level, $last, $dirs,$files);
?>
<strong>Sub Directories in: <?php echo substr($start_dir, 2); ?></strong><br>
sort($dirs);
if(empty($dirs)) // checks if the dirs array is empty. if so, then display "empty"
{
echo"";
}
/* SHOWS THE DIRECTORIES FROM ARRAY */
foreach($dirs as $dir)
{
echo "<a href=\"$PHP_SELF?i=$dir\">$d</a<br> ";
}
}
?>
<?php
/* SHOWS FILES FROM ARRAY*/
$tf = count($files);
if($tf != 0){
echo "<br>Total Files: $tf<br><br>"; /* Display total in directory*/
}
sort($files); // Sort the files alphabetically
$i = 1;
foreach($files as $file)
{ //Below PHP functions are used to display file stats such as creation time, permissions etc.
$f = preg_replace(‘/^.*\/\s*/’, ”, $file);
echo $i;
echo ". <a href=\"$file\">$f</a>";
echo " size:".get_size($file)."<br>";
}
[/code]