Page 1 of 1

Testing for an empty file field

Posted: Thu Nov 06, 2003 5:33 pm
by evilmonkey
Hello.

My code seems to be behaving oddly in terms of if's...(take a look):

Code: Select all

<?php
//check if an avatar was entered
if ($_FILES['avatar']['name'] != "" ) {
$avinfo=getimagesize($_FILES['avatar']['tmp_name']);
if ($avinfo['0'] < 41 && $avinfo['1'] < 41) {
move_uploaded_file($_FILES['avatar']['tmp_name'], 'avatars/'.$_FILES['avatar']['name']); }
//check for correct input
if ($name == "" || $email == "") 
{
echo "Something is wrong. Please go back and make sure you have filled in all required boxes.";
//HTML redirection
?><meta http-equiv="refresh" content="3;url=profile.php"><?php
}
else 
{
$avname=$_FILES['avatar']['name'];
if ($pass2=="") {
$query="UPDATE users SET name='$name', email='$email', icq='$icq', msn='$msn', aim='$aim', location='$location', greeting='$greeting', avatar='$avname' WHERE id='$id'"; }
elseif ($pass2=="" && $avname=="") {
$query="UPDATE users SET name='$name',  email='$email', icq='$icq', msn='$msn', aim='$aim', location='$location', greeting='$greeting' WHERE id='$id'"; }
elseif ($avname=="") {
$pass2=md5($pass2);
$query="UPDATE users SET name='$name', password='$pass2', email='$email', icq='$icq', msn='$msn', aim='$aim', location='$location', greeting='$greeting' WHERE id='$id'"; }
else {
$pass2=md5($pass2);
$query="UPDATE users SET name='$name', password='$pass2', email='$email', icq='$icq', msn='$msn', aim='$aim', location='$location', greeting='$greeting', avatar='$avname' WHERE id='$id'"; }
$result=mysql_query ($query, $db) or die (mysql_error());
echo "You just altered your profile. Please wait...";
//HTML redirection 
?>
<meta http-equiv="refresh" content="3;url=index.php">
        <?php
} }
else {
echo "Your avatar is too large. Please go back and choose another one.";
?><meta http-equiv="refresh" content="3;url=profile.php"><?php } ?>
The problem is, when an avatar is not submitted (I test it using if ($_FILES['avatar']['name'] != "" ) { do stuff} ), it returns "Your avatar is too large. Please go back and choose another one". Something is terms of if's and else's is messed up, I need another set of eyes, I know it's some dumb error. If an avatar is entered, everything works fine.

I apologize for the messy code.

Thanks for the help!

Posted: Thu Nov 06, 2003 5:36 pm
by Gen-ik
Try using if(strlen($var) > 0) instead of if($var != "")

Posted: Thu Nov 06, 2003 8:29 pm
by evilmonkey
Hello Gen-ik. I'm afraid that had no effect and the problem remains. Any other ideas?

Cheers!

Posted: Thu Nov 06, 2003 8:59 pm
by McGruff
Try a print_r($_FILES) at the top of the script to help debug.

Perhaps you should be using $_POST['name'] instead of $name, etc?

Posted: Thu Nov 06, 2003 9:13 pm
by volka
there many thing you can test before accessing the name, e.g.

Code: Select all

if ($_FILES['avatar']['size'] < 1
		|| $_FILES['avatar']['error']
		|| !is_uploaded_file($_FILES['avatar']['tmp_name'])
	)
{ 
	/** something went wrong or no file has been uploaded */
}