Jump to content

PHP int compare problem


Rheves

Recommended Posts

I'm working on a file upload script and have the following code:

 

if ($_FILES["filename"]["error"] > 0){
  if ($_FILES["filename"]["error"] == 1){
      $FileCheck = "Failure, file size too large.";
  }
  echo "Error: " . $_FILES["filename"]["error"] . "<br />";
  return false;
         }else{
  //Do upload
	return true;
}

 

The line checking that error = 1 never evaluates to true, but the following echo statement spits out 1. I tried doing a "$_FILES["filename"]["error"] * 1" to force it to be an int but that didn't help. This seems very simple but I can't figure out why it isn't comparing properly?

Link to comment
Share on other sites

I was checking the value of $FileCheck afterwards to check if the code had been executed, but it was always empty. I threw in some echoes and my mistake that code is being evaluated properly, but $FileCheck isn't being set properly.

 

My code that calls the function is as follows and $FileCheck is always empty:

if (SendIt()){//The function above
	//Worked
	$MessageToSend .= "\n
	Uploaded File: "(URL)/" . $_FILES["filename"]["name"];
	$FileCheck = "<tr><td>File Upload:</td>
					  <td>Success!</td></tr>";
}else{
	//Didn't
	echo "send it returned false, FileCheck is: " . $FileCheck;
}

Link to comment
Share on other sites

Sorry, they are valid in my script, I was just removing the actual url being used:

 

if (SendIt()){//The function above
	//Worked
	$MessageToSend .= "\n
	Uploaded File: (URL)/" . $_FILES["filename"]["name"];
	$FileCheck = "<tr><td>File Upload:</td>
					  <td>Success!</td></tr>";
}else{
	//Didn't
	echo "send it returned false, FileCheck is: " . $FileCheck;
}

 

Is what it should be, the problem isn't there.

Link to comment
Share on other sites

nevermind, misread.

 

I saw your post before you edited it and passing it by reference fixed it, even though I had $FileCheck declared at the very top of my script, Though I am now getting the following error:

 

PHP Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of SendIt(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in [...] on line 137

 

But I can work from there, thank you!

 

I'll try to post more of my source code if I need any help in the future too, I kind of made a mess by only posting a little bit.

 

Edit: The above was caused by my putting the & in front of where I called SendIt instead of where I declare the function.

Link to comment
Share on other sites

Okay, so I was understanding it properly.

 

Wish I could take back that edit :D

 

You have to change how you've declared your function. You have to add the argument when you declare the function

 

<?php

function testIt(&$message) {

// Some test that could be true or false
if( rand(0,1) ) {
	$message = 'It was successful';
	return true;
} else {
	$message = 'It failed';
	return false;
}

}

if(testIt($response)) {
echo 'Function returned true, message was: ' . $response;
} else {
echo 'Function returned false, message was: ' . $response;
}

?>

 

function testIt(&$message)

 

Please, show us how you're using it, and how you've declared the SendIt function

Link to comment
Share on other sites

I was getting that error when I called it like this:

 

if (SendIt(&$FileCheck)){
    //Worked
}else{
//Didn't
}

 

But removing that & and changing the declare function to read this instead:

 

function SendIt(&$EM){
[...]
}//I changed it to EM within the function

 

Fixed it.

Link to comment
Share on other sites

Perfect! Don't forget to read up on variable scope! It's important to know how it works when developing. Keep in mind some languages treat scope slightly differently, but the basic concepts are always the same.

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.