[SOLVED] recursive file list, and recursive directory list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

[SOLVED] recursive file list, and recursive directory list

Post 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
Last edited by batfastad on Thu Jul 28, 2005 1:38 pm, edited 1 time in total.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post 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
Last edited by batfastad on Thu Jul 28, 2005 1:10 pm, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Excellent.

Another thing I've learnt about PHP


Thanks for all your help
Post Reply