Page 1 of 1

False false returned?

Posted: Mon Dec 19, 2011 6:58 pm
by rick.emmet
Hi Everyone,
I'm testing out some functionality and can't get the results I'm expecting. On a page that will process uploaded data and photos, I call a function from one of my libraries that will count the number of photos uploaded. That function ( countPhotos), first determines if no photos were uploaded, and if that's the case, returns False.

The library is an Included file and I've been reading the manual on Return and they say that if the Return comes from an Include file, control is returned to the file that calls the function. This is a bit confusing to me. Here's my script:

Code: Select all

//	THIS IS THE FUNCTION CALLING "countPhotos"

	// Count the number of files uploaded
	$photo_num = countPhotos();
		if (false) 	{	
			echo "<div id='mainContent'>
     			<span class='browserTable'> 
      			<br />
      			<p>
				You did not upload any photos.<br />
				<a href='paid_bicycle_form.php'>BACK</a>
      			</p>									
      			</span>
      			</div>";
				//Display bottom wrapper
				main_wrpr_bottom();
				// display the footer
				do_html_footer();
				exit;
				
		} else	{
			
			// Continue - the count will be used in all the photo processing functions
			echo "The number of photos is ".$photo_num;
}

		
		//	THIS IS WITHIN AN INCLUDE LIBRARY FILE

function countPhotos()	{	
	// How many photos have been uploaded?
	if ($_FILES['userfile']['error'][0] == 4 && $_FILES['userfile']['error'][1] == 4 &&
		$_FILES['userfile']['error'][2] == 4 && $_FILES['userfile']['error'][3] == 4 &&
		$_FILES['userfile']['error'][4] == 4)	{
		
		return false;
		
	} else	{ 
	
	$count_photos = 0;
		foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name)	{
			if (is_uploaded_file($tmp_name))	{
				$count_photos++;
			}
		}
		return $count_photos;
	}
}
If I select five photos and click on the submit button, I get the failure notice, “You did not upload any photos”. This is exactly the opposite behavior expected. If I click on the submit button without uploading any files at all (no photos), I get a message, “The number of photos is 0". This comes from the portion of the function, that calls countPhotos, below the Else statement; and would only be expected to be output if at least one photo (or file) was uploaded.

I guess what is confusing me so much is that if the function doesn't receive the False from countPhotos, why would it execute the portion of the script the outputs the message, “You did not upload any photos”. Does anybody know what I'm doing wrong here? I very much appreciate your input!
Cheers,
Rick

Re: False false returned?

Posted: Tue Dec 20, 2011 7:51 am
by Dodon
You can't do if(false), that basically means the same as skip this statement and go to the else statement (if you got an else statement ofc).

You can compare the value you get to false

Code: Select all

$photo_num = countPhotos();
    if ($photo_num == false)      { 
 
This is assuming your function countphotos is working correctly.

Re: False false returned?

Posted: Tue Dec 20, 2011 1:55 pm
by rick.emmet
Hello Dodon,
Thanks for the reply, that works. I really appreciate the help. We didn't cover any of this in class, but I sure am glad the folks here at DevNetwork are willing to lend a hand.
Cheers,
Rick