<html>
<head>
<title>Lab 9 - Ryan Small</title>
</head>
<body>
<p>Currently working in the <?php echo getcwd();?> directory.</p>
<pre>
<?php
// $debug=true;
$dir_depth=0;
function readdirectory($directstring)
{
global $debug, $dir_depth;
$dir_depth++;
if($debug)echo "!! Call to readdirectory ($directstring)...\n";
//$dirhandle is the variable that stores which directory to read from
//This statement acquires the current working directory to read from and places it in $dirhandle
$dirhandle = opendir($directstring);
//readdir() returns a false value when it can't read a directory, so this statement will continue to
//loop until that time when readdir() returns false. That will happen when it finishes reading the directory
//or if it can't read it at all.
//$directory is the variable that is used to hold the name of what it readdir() is reading.
clearstatcache();
while( false!== ($directory = readdir($dirhandle)))
{
if($debug)echo "!! Found object: {$directory} is_dir:" . is_dir($directory) . " is_file:". is_file($directory). "\n";
//this if statement checks to see if the file that was read was a directory or a file
if (is_dir($directory))
{
//if it was a directory, it parses through and ignores the directories "." and ".."
//"." is a self reference, and ".." is a reference to the parent directory. It ignores
//these because there's no use in displaying them.
if ( $directory != "." && $directory != ".." )
{
//Now we start displaying the names of the directories that were found with readdir()
for($i=0;$i<$dir_depth;$i++) echo "\t";
echo $directory . ":\n";
$nest_directstring = getcwd() . "/" . "$directory";
chdir($nest_directstring);
readdirectory($nest_directstring);
}
}
if(is_file($directory))
{
//Displays the files for linking
for($i=0;$i<$dir_depth;$i++) echo "\t";
echo "<a href=" . $directory . ">" . $directory . "</a><br />";
}
}
//must specify handle or closes all handles, i think
closedir($dirhandle);
if($debug)echo "!! leaving readdirectory ($directstring)\n";
chdir('..');
$dir_depth--;
}
echo "<br />Directories:<br />";
readdirectory(getcwd());
?>
</pre>
</body>
</html>