Looks like you are trying to list subdirectories as well?
That wont work this way.
If you want to list subdirectories you basically have to include the directory listing code for every suibdirectory level that you may find, which is ofcourse
silly.
Instead, use recursion.
Change your code into a function that you can call with just a directoryname as parameter.
This function would then list all files in that directory.
WHen if finds a subdirectory, it would call a function to list that directory.
The function called would be the same function that is listing the current directory, like this in pseudo code:
function list_dir($dirname)
{
read the current dir
while $filename=readdir()
{
is this a directory?
if is_dir($filename)
{
yes it is a directory, call myself to read this directory
list_dir($filename);
}
else
{
It's a file, so print it.
print $filename;
};
};
};