Friday, February 08, 2008 Vienna 0 Comments
I've seen lots of people asking for this, so I'm posting a recursive function that iterates through a directory and subdirectories, and lists all the files contained within.
This function takes one parameter, the directory you want to list. The function then continues looping through itself until it runs out of subdirectories and files within this directory. The function returns a string containing the files and folders contained in the directory displayed in a tree.
The script is pretty straight forward.
function list_directory($dir)
{
$printout = "";
if(is_dir($dir))
{
if($d = opendir($dir))
{
while(($filename = readdir($d)) !== false)
{
if($filename != "." && $filename != "..")
{
$file = "$dir/$filename";
$path_info = pathinfo($file);
$type = is_dir($file) ? "DIR" : strtoupper($path_info["extension"]);
$size = formatSize(filesize($file));
$printout .= "";
if($type == "DIR")
{
$printout .= "" . basename($file) . "";
}
else
{
$printout .= basename($file) . " - $size";
}
$printout .= "";
if($type == "DIR")
{
$printout .= list_directory($file);
}
$printout .= "";
unset($style, $size);
}
}
closedir($d);
}
}
return $printout;
}
function formatSize($s)
{
$s = $s/1000;
if($s < 1)
{
$s = $s * 1000;
$units = "bytes";
}
elseif($s < 1000)
{
$s = round($s);
$units = "KB";
}
else
{
$s = round($s/1000, 2);
$units = "MB";
}
$size = "$s $units";
return $size;
}
So if we were to do this...
$dir_list = list_directory("Dummpy");
echo $dir_list;
We could get something like this...
Books
Biographies
Copy of Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Science Fiction
Copy (2) of Copy of Copy of Dummy file.txt - 0 bytes
Copy (2) of Copy of Dummy file.txt - 0 bytes
Copy (2) of Dummy file.txt - 0 bytes
Copy (3) of Copy of Dummy file.txt - 0 bytes
Copy (3) of Dummy file.txt - 0 bytes
Copy (4) of Dummy file.txt - 0 bytes
Copy (5) of Dummy file.txt - 0 bytes
Copy (6) of Dummy file.txt - 0 bytes
Copy of Copy of Copy of Dummy file.txt - 0 bytes
Copy of Copy of Dummy file.txt - 0 bytes
Copy of Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Travel
Copy (2) of Dummy file.txt - 0 bytes
Copy of Copy of Dummy file.txt - 0 bytes
Copy of Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Copy (2) of Dummy file.txt - 0 bytes
Copy of Copy of Dummy file.txt - 0 bytes
Copy of Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Movies
Action
Copy (2) of Dummy file.txt - 0 bytes
Copy (3) of Dummy file.txt - 0 bytes
Copy (4) of Dummy file.txt - 0 bytes
Copy (5) of Dummy file.txt - 0 bytes
Copy of Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Comedy
Copy of Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Drama
Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Music
Country
Dummy file.txt - 0 bytes
Gospel
Rock
Alternative
Dummy file.txt - 0 bytes
Punk
Copy (2) of Dummy file.txt - 0 bytes
Copy (3) of Dummy file.txt - 0 bytes
Copy of Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Copy of Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes
Dummy file.txt - 0 bytes




