Recursive function problem

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
joape382
Forum Newbie
Posts: 1
Joined: Tue Oct 12, 2004 2:33 pm

Recursive function problem

Post by joape382 »

twigletmac | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

I have the following function that is supposed to loop through the current directory and all subdirectories and display all gif and jpg image files. However, it only loops through one or two directories and then it can't recognize any more directories (even though there are more). What is wrong with the function?

Code: Select all

function recursiveDirInclude( $currentDir, $level ) {
	$d = dir( $currentDir );
	while( $entry = $d->read() ) { //Go through the directory
		echo "$d->path/$entry - $level<BR>";
		if( $entry == ".." || $entry == "." )
			continue;
		if( is_file( $d->path."/".$entry ) && isPicture( $entry ) ) { //An image: show it and return
			$textPictureName = "";
			$textPictureDescr = "";
			if( ( $fp = @fopen( stripFileExtension( $d->path."/".$entry ).".txt", "r" ) ) != false ) {
				if( $_SESSION[LANGUAGE_VARIABLE_NAME] == LANGUAGE_ENGLISH ) {
					$textPictureName = fgets( $fp );
					fgets( $fp );  //Discard the swedish text
					$textPictureDescr = fgets( $fp );
				} else {
					fgets( $fp );  //Discard the english text
					$textPictureName = fgets( $fp );
					fgets( $fp );  //Discard the english text
					$textPictureDescr = fgets( $fp );
				}
				fclose( $fp );
			}
			echo "  <A href='".$currentDir."/".$entry;

			//GET & PRINT THE current URL (EXCL filename!) !!!!!!

			echo "' class='link' target='_blank' onMouseOver="return setStatusMsg('";
			if( $_SESSION[LANGUAGE_VARIABLE_NAME] == LANGUAGE_SWEDISH ) 
				echo "Klicka för att se bilden";
			else
				echo "Click to see the picture";
			echo "')" onMouseOut="return setStatusMsg('')">\n";
			if( $textPictureName == "" )
				echo "$entry</A>";
			else
				echo "$textPictureName</A> - $textPictureDescr";
			echo "<BR>\n";
		} else if( is_dir( $d->path."/".$entry ) ) { //A dir: show as text and go into directory
//		} else if( ((@fileperms("$d->path."/".$entry") & 0x4000) == 0x4000) ) { //A dir: show as text and go into directory
			$textDirName = "";
			$textDirDescr = "";
			if( ( $fp = @fopen( $d->path."/".$entry.".txt", "r" ) ) != false ) {
				if( $_SESSION[LANGUAGE_VARIABLE_NAME] == LANGUAGE_ENGLISH ) {
					$textDirName = fgets( $fp );
					fgets( $fp );  //Discard the swedish text
					$textDirDescr = fgets( $fp );
				} else {
					fgets( $fp );  //Discard the english text
					$textDirName = fgets( $fp );
					fgets( $fp );  //Discard the english text
					$textDirDescr = fgets( $fp );
				}
				fclose( $fp );
			}
			echo "  <H".( $level + 1 ).">";
			if( $textDirName == "" )
				echo "$entry</H".( $level + 1 ).">";
			else {
				echo "$textDirName</H".( $level + 1 ).">";
				if( $textDirDescr != "" )
					echo "<P>$textDirDescr</P>";
			}
			
			//Go into dir
			recursiveDirInclude( $d->path."/".$entry, $level + 1 );
		} else echo "---> NEITHER: ".$d->path."//$entry<BR>";
	}
	chdir( ".." );
}
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

remove chdir('..') line.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It may be easier to debug if you strip the function down to the barest minimum and get that working - then reintegrate it.

Mac
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

We've had several threads in my short history here involving folder traversal finding images.. try searching:

[devnet]folder* OR director* +tree[/devnet]
Post Reply