Jump to content

False false?


rick.emmet

Recommended Posts

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:

 

//	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

Link to comment
Share on other sites

Thank you, Thorpe, I tried the following code and got it to work:

 

$photo_num = countPhotos();
	if ($photo_num == false) 	{

 

We didn't cover this in class, which seem very strange (we were trying to cover almost all of "PHP MySQL Web Development"). I'll be using this all the time, so I appreciate the input!

Cheers,

Ricki

Link to comment
Share on other sites

Most developers would simplify this to:

 

$photo_num = countPhotos();
	if (!$photo_num) {
                   // false
                } 

 

You can also simplify it even further:

 

if (!$photo_num = countPhotos()) {
  // false
}

 

You might want to read the php manual on boolean conversion: http://us.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.