[SOLVED] recursive file list, and recursive directory list
Moderator: General Moderators
[SOLVED] recursive file list, and recursive directory list
Hi guys
I'm trying to build a file search utility.
I've tried using a recursive glob function (from the PHP man page) but it has problems with & ampersand characters in the file and directory names.
So I need to try and find a different way of doing it.
Basically I need to end up with two arrays, one with all the directories and sub-directories, and another with all the files.
They both need to be recursive.
It would be better if it was one recursive function, which appended files to a files array, and directories to a directory array.
That would give better performance as it would only have to loop through the 10,000 files once, rather than once for files and again for the directories.
I've got the actual searching of the array sorted.
Using the recursive glob function, performance was good with 10,000 files being returned.
Search results took about 3-6 seconds which isn't bad.
I'm using this on my home LAN, on a windows platform.
Any ideas?
Thanks
Ben
I'm trying to build a file search utility.
I've tried using a recursive glob function (from the PHP man page) but it has problems with & ampersand characters in the file and directory names.
So I need to try and find a different way of doing it.
Basically I need to end up with two arrays, one with all the directories and sub-directories, and another with all the files.
They both need to be recursive.
It would be better if it was one recursive function, which appended files to a files array, and directories to a directory array.
That would give better performance as it would only have to loop through the 10,000 files once, rather than once for files and again for the directories.
I've got the actual searching of the array sorted.
Using the recursive glob function, performance was good with 10,000 files being returned.
Search results took about 3-6 seconds which isn't bad.
I'm using this on my home LAN, on a windows platform.
Any ideas?
Thanks
Ben
Last edited by batfastad on Thu Jul 28, 2005 1:38 pm, edited 1 time in total.
This is driving me insane.
Ok I've come up with the following function from snippets of code in the manual pages...
But for some reason my file array $f_arr and $d_arr are empty.
The function works, as I get a recursive listing displayed if I uncomment my echo lines.
I'm not too hot on arrays yet and have trouble understanding all the ways you can start them and set the data.
Anyone spot where I've gone wrong?
Thanks
Ben
Ok I've come up with the following function from snippets of code in the manual pages...
Code: Select all
<?php
$d_arr = array();
$f_arr = array();
$BASEDIR="I:/";
function recursedir($BASEDIR) {
$hndl=opendir($BASEDIR);
while($file=readdir($hndl)) {
if ($file=='.' || $file=='..') continue;
$completepath="$BASEDIR/$file";
if (is_dir($completepath)) {
# its a dir, recurse.
recursedir($BASEDIR.'/'.$file);
// echo "DIR: $BASEDIR/$file<br>\n";
$d_arr[] = $file."||||".$BASEDIR;
} else {
# its a file.
// echo "FILE: $BASEDIR/$file<br>\n";
$f_arr[] = $file."||||".$BASEDIR;
}
}
}
recursedir($BASEDIR);
echo "<pre>\n";
print_r($f_arr);
echo "\n</pre>";
echo "<pre>\n";
print_r($d_arr);
echo "\n</pre>";
?>The function works, as I get a recursive listing displayed if I uncomment my echo lines.
I'm not too hot on arrays yet and have trouble understanding all the ways you can start them and set the data.
Anyone spot where I've gone wrong?
Thanks
Ben
Last edited by batfastad on Thu Jul 28, 2005 1:10 pm, edited 1 time in total.
Read: http://us3.php.net/globaljshpro2 wrote:Your problem is probably you either need recursedir to return something or make use of global variables.
Awesome - sorry I didn't pay attention to that bit.
Ok I'd never heard of global variables and the scope of variables before, but a quick read and I'd worked out what I needed to do
I added the following between lines 8 and 9...
And it works.
I can return my two arrays.
Thanks so much for your help on this.
But I have one last question...
Why do I have to turn $d_arr and $f_arr into global variables, but I can just leave $BASEDIR without being a global?
Thanks
Ben
Ok I'd never heard of global variables and the scope of variables before, but a quick read and I'd worked out what I needed to do
I added the following between lines 8 and 9...
Code: Select all
global $d_arr, $f_arr;I can return my two arrays.
Thanks so much for your help on this.
But I have one last question...
Why do I have to turn $d_arr and $f_arr into global variables, but I can just leave $BASEDIR without being a global?
Thanks
Ben
batfastad wrote:Why do I have to turn $d_arr and $f_arr into global variables, but I can just leave $BASEDIR without being a global?
Code: Select all
function no_return($var) {
$var++;
}
echo no_return(5); // outputs 5Code: Select all
function yes_return($var) {
$var++;
return($var);
}
echo yes_return(5); // outputs 6Code: Select all
global $var;
$var = 5;
function global_no_return($var) {
$var++;
}
echo global_no_return(); // outputs 6