Page 1 of 1

[SOLVED] recursive file list, and recursive directory list

Posted: Wed Jul 27, 2005 4:18 am
by batfastad
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

Posted: Thu Jul 28, 2005 1:06 pm
by batfastad
This is driving me insane.
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>";
?>
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

Posted: Thu Jul 28, 2005 1:10 pm
by josh
You might want to write a recursive function that uses glob, and use a foreach to check if any of the items returned by glob were folders. Your problem is probably you either need recursedir to return something or make use of global variables.

Posted: Thu Jul 28, 2005 1:12 pm
by batfastad
For some reason using glob() seams to return blank lines in the array when it finds folders and files with ampersands & symbols in the names.


I think I'm close with this, I just can't get the listing into the arrays.
And I'm pretty sure it's because I'm not good with arrays yet.

Posted: Thu Jul 28, 2005 1:16 pm
by josh
jshpro2 wrote:Your problem is probably you either need recursedir to return something or make use of global variables.
Read: http://us3.php.net/global

Posted: Thu Jul 28, 2005 1:24 pm
by batfastad
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...

Code: Select all

global $d_arr, $f_arr;
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

Posted: Thu Jul 28, 2005 1:30 pm
by josh
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 5

Code: Select all

function yes_return($var) {
     $var++;
     return($var);
}

echo yes_return(5); // outputs 6

Code: Select all

global $var;
$var = 5;
function global_no_return($var) {
     $var++;
}
echo global_no_return(); // outputs 6
It explains this in the link I posted, also check out http://us3.php.net/return

Posted: Thu Jul 28, 2005 1:38 pm
by batfastad
Excellent.

Another thing I've learnt about PHP


Thanks for all your help