Jump to content

[SOLVED] recognising blank field


chocopi

Recommended Posts

I have an avatar upload field in my form and I am trying to see if it is empty and if so then skip the validation. Here is my code

 

<form name="edit_profile" method="post" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data">
<input type="file" name="avatar" id="avatar" value="" />
<input type="submit" name="submit" id="submit" value="Submit" />


<?php

if($_POST)
{
	// validation for avatar
	// set avatar directory
	$directory = 'avatars/';
	// set max file size
	$max_file_size = '10000';
	// set allowed extensions (for more, just add a comma then 'image/whatever'
	$allowed_files = array('image/gif');
	// get avatar information
	// get avatar name
	$file_name = $_FILES['avatar']['name'];
	// get avatar size
	$file_size = $_FILES['avatar']['size'];
	// get avatar type image/whatever
	$file_type = $_FILES['avatar']['type'];
	// get file extension of $file_type this is only if there are more than 1 extension allowed
	list($blank,$file_extension) = explode('image/',$file_type);
	// check if there is a selection by checking if either the name, type or size is empty
	if(empty($file_size) or empty($file_name) or empty($file_type))
	{
		echo "";
	} else
	{
		// check file size is not bigger than max
		if($file_size > $max_file_size)
		{
			// if avatar is too big, give error
			echo "Your file is larger than 10kb.<br />";
			$errors++;
		}
		// check the avatar has the correct extension
		if(!in_array($file_type, $allowed_files))
		{
			// if wrong extension, give error
			echo "You are not allowed to upload that file type.<br />You are only allowed: .gif<br />";
			$errors++;
		}
		if($errors == 0)
		{
			// get old image
			$old_file = $directory.''.$page_id.'.gif';
			// check if old avatar exists
			if(file_exists($old_file))
			{
				// if avatar exists delete it
				unlink($old_file);
			}
			// copy avatar to directory
			copy($_FILES['avatar']['tmp_name'], "".$directory."".$_FILES['avatar']['name']) or die("Your avatar could not be copied correctly");
			// rename the file to the user id so it can be pulled in other files easily
			rename("".$directory."".$_FILES['avatar']['name'], "".$directory."".$page_id.".".$file_extension);
		}
	}

}

?>

 

I think that is all of the code. The problem is I only want for the validation, and updating to be done if something is in the field. I currently am not using:

 

$_POST['avatar'];

 

and then using validation on it because I was originally but it was not working :(

So I am now trying to check the if either the $file_name, $file_size or $file_type are empty with this line:

 

if(empty($file_size) or empty($file_name) or empty($file_type))

 

And I know from testing that if the field is left blank then the name and size will be blank and size will be 0 and these are all covered by empty(). But even if I put something into this field it will skip it :(

 

Any help would be greatly appreciated ;D

 

If you need any more information, just ask,

 

PS sorry for all the tabbing in the code

 

~ Chocopi

Link to comment
Share on other sites

kool thanks Orio, I knew there would be something to make it simple ;D

 

I have this

 

if($var['error'] == 4)

 

but what is the $var actually meant to be out of all my other variables ?

 

~ Chocopi

Link to comment
Share on other sites

ok i am stumped, i have been messing with this code all day, and when i finally get it working, the copy stops  >:(

 

I have even tried using move_uploaded_file() instead of copy() but to no luck, so if someone can check my code to see if there are any mistakes that i have missed. i know it runs the copy as i have put echos before and after it.

 

Thanks,

 

~ Chocopi

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.