Home > Uncategorized > Recursively listing directories in PHP

Recursively listing directories in PHP

Just a simple PHP script to list directories two level deep:

$dir = dirname(__FILE__).’/’;

// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if (filetype($dir . $file) == “dir” && $file !== “.” && $file !== “..” && $file !== “images”)
{
echo “<li>$file</li>”;
$dh2 = opendir($dir . $file);
echo “<ul>”;
while (($file2 = readdir($dh2)) !== false)
{
if ($file2 !== “.” && $file2 !== “..”)
{
echo “<li><a href=’$file/$file2′>$file2</a></li>”;
}
}
closedir($dh2);
echo “</ul>”;

}
}
closedir($dh);
}
}

I’ve used this to help index the 80 GB or so files I’ve now recovered from the GEOCITIES torrent

Which I’ve indexed from A to Z 

     Sunday, October 14, 2012 11:17 AM        <dir> 0
     Monday, October 15, 2012  3:10 PM        <dir> 1
     Sunday, October 14, 2012 11:18 AM        <dir> 2
     Sunday, October 14, 2012 11:19 AM        <dir> 3
     Sunday, October 14, 2012 11:20 AM        <dir> 4
     Sunday, October 14, 2012 11:20 AM        <dir> 6
     Sunday, October 14, 2012 11:20 AM        <dir> 8
     Sunday, October 14, 2012 11:20 AM        <dir> 9
     Monday, October 15, 2012  3:09 PM        <dir> _
     Monday, October 15, 2012  4:53 PM        <dir> a
     Monday, October 15, 2012  5:06 PM        <dir> b
     Monday, October 15, 2012  5:21 PM        <dir> c
     Sunday, October 14, 2012  3:33 PM        <dir> d
    Tuesday, October 16, 2012  3:37 AM        <dir> e
     Monday, October 15, 2012  3:54 AM        <dir> f
    Tuesday, October 16, 2012  3:48 AM        <dir> g
    Tuesday, October 16, 2012  3:50 AM        <dir> h
    Tuesday, October 16, 2012  4:35 AM        <dir> i
    Tuesday, October 16, 2012  4:54 AM        <dir> j
     Monday, October 15, 2012  5:38 AM        <dir> k
    Tuesday, October 16, 2012  5:55 AM        <dir> l
     Monday, October 15, 2012  7:40 AM        <dir> m
     Monday, October 15, 2012  7:47 AM        <dir> n
     Monday, October 15, 2012  9:36 AM        <dir> o
     Monday, October 15, 2012  9:43 AM        <dir> p
     Monday, October 15, 2012 12:20 PM        <dir> r
     Monday, October 15, 2012 12:45 PM        <dir> s
     Monday, October 15, 2012 12:52 PM        <dir> t
     Monday, October 15, 2012  2:26 PM        <dir> u
     Monday, October 15, 2012  2:30 PM        <dir> v
     Monday, October 15, 2012  2:34 PM        <dir> w
     Monday, October 15, 2012  2:34 PM        <dir> x
     Monday, October 15, 2012  2:50 PM        <dir> y
     Monday, October 15, 2012  2:59 PM        <dir> z
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: