Greetings.
I've came up with the following code to recursively read directories and files, but something odd is going on.
(Notice that the commands starting on the commented line are there because PHP couldn't recognize some files/dirs as such).
The counting variables (both for files and directories) are not being incremented as they should.
So, I always get the correct files/dirs listing, but I never get anything different from "0 file(s) in 0 dir(s)".
Can someone point out what I'm doing wrong?
I'm running this on PHP 5.1.2 on Apache 2.0.55 / Windows XP Professional:
<?php
global $myFilePath;
global $fileCount;
global $dirCount;
$fileCount = 0;
$dirCount = 0;
$myFilePath = '';
$filePath = 'C:\foo\bar';
function readFolder($fld)
{
$myFilePath = $fld;
echo "<TR><TD COLSPAN='2' BGCOLOR='#CECECE'>$myFilePath Contents</TD></TR>";
$dh = opendir($myFilePath) or die();
while (false !== ($file = readdir($dh)))
{
if ($file != '.' and $file != '..')
{
echo "<TR><TD>$file</TD><TD>";
if (is_dir($file))
{
$dirCount++;
echo "DIR";
echo "</TD></TR>";
readFolder($myFilePath."\\".$file);
}
else if (is_file($file))
{
$fileCount++;
if (preg_match('/\.php$/i', $file))
{
echo "<FONT COLOR='#000080'>PHP FILE</FONT></TD></TR>";
}
else
{
echo "FILE";
echo "</TD></TR>";
}
}
else
{
// See Notice above
if ($adh = @opendir($myFilePath."\\".$file))
{
$dirCount++;
echo "DIR";
echo "</TD></TR>";
closedir($adh);
readFolder($myFilePath."\\".$file);
}
else
{
$fileCount++;
if (preg_match('/\.php$/i', $file))
{
echo "<FONT COLOR='#000080'>PHP FILE</FONT></TD></TR>";
}
else
{
echo "FILE";
echo "</TD></TR>";
}
}
}
}
}
closedir($dh);
}
echo <<<HTML
<HTML>
<HEAD>
<STYLE TYPE="text/css">
td
{
font-family: Verdana;
font-size: 12px;
}
</STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
HTML;
echo "<TABLE WIDTH='100%' BORDER='1'>";
readFolder($filePath);
echo "<TR><TD COLSPAN='2'>$fileCount file(s) in $dirCount dir(s).</TD></TR>";
echo "</TABLE>";
echo <<< HTML
</FORM>
</BODY>
</HTML>
HTML;
?>