Hello,
I am trying to recursively list files in a directory. I can get this to work with almost no problems. The function I have takes an inpu path, and it recursively lists the directories and/or files underneath the root file that I pass in.
So what's the problem, then? Well, let's say I have the following root directory:
c:\mydir
c:\mydir\a (nested directory)
c:\mydir\b.txt
c:\mydir\c (nested directory)
Currently, it lists them all in alphabetic order, so it would print the following:
c:\mydir
c:\mydir\a
c:\mydir\b.txt
c:\mydir\c
I want to be able to separate the files from the directory list so that it would look like this when printed:
c:\mydir
c:\mydir\b.txt
c:\mydir\a
c:\mydir\c
I am using the following function:
function getDirFiles($dirPath,$depth=0) {
if ($handle = opendir($dirPath)) { // make sure it's a directory
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($dirPath."\\".$file)) {
echo "directory: " . $dirPath . "\\" . $file . "\n";
getDirFiles($dirPath."\\".$file,($depth+1));
}
else { // is file, not directory
echo "file: " . $dirPath . "\\" . $file . "\n";
}
}
}
// Close the dir handle
closedir($handle);
}
}
Can someone please let me know how I can do this?