Testing for an empty file field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Testing for an empty file field

Post 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!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Try using if(strlen($var) > 0) instead of if($var != "")
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Hello Gen-ik. I'm afraid that had no effect and the problem remains. Any other ideas?

Cheers!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 */
}
Post Reply