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
https://forums.phpfreaks.com/topic/267191-php-int-compare-problem/
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;
}

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.

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.

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.