Page 1 of 1

Recursive function problem

Posted: Tue Oct 12, 2004 3:30 pm
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( ".." );
}

Posted: Tue Oct 12, 2004 3:58 pm
by Weirdan
remove chdir('..') line.

Posted: Wed Oct 13, 2004 3:48 am
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

Posted: Sun Oct 17, 2004 6:44 pm
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]