Link to home
Start Free TrialLog in
Avatar of FairyBusiness
FairyBusinessFlag for United States of America

asked on

How to tell from a form field if an image value was entered?

Hi, I am trying to create a function that only tries to upload a photo if a new photo was selected:

function upload() {
	global $errors;
	if((isset($_POST['image'])) && (empty($errors))) {
		if(isset($_GET['edit'])) {
			$table = $_GET['edit'];
		}
		if(isset($_POST['table'])) {
			$table = mysql_clean_strings($_POST['table']);
		}
		echo "hello";
		$image = explode('.',basename($_FILES['image']['name']));
		$image = $image[0];		
		$file = basename($_FILES['image']['name']);	
		$temp_path = $_FILES['image']['tmp_name'];
		$target_path = $table . "/" . $image;
		move_uploaded_file($temp_path, $target_path);
	}
	elseif(isset($_GET['item'])) {
		echo "hi";
		$image = $_GET['item'];
	}
	else {
		$image = "";
	}
	return $image;
}

Open in new window


I did have:

      if((isset($_FILES['image'])) && (empty($errors))) {

but it always thinks there was a file to upload even if there is not value in the form field.  But when I use post it never thinks there is a file to upload.  What do I do?

I just tried this:
function upload() {
	global $errors;
	if((isset($_FILES['image'])) && (!empty($_FILES['image']))) {
		var_dump($_FILES['image']);
		if(isset($_GET['edit'])) {
			$table = $_GET['edit'];
		}
		if(isset($_POST['table'])) {
			$table = mysql_clean_strings($_POST['table']);
		}
		echo "hello";
		$image = explode('.',basename($_FILES['image']['name']));
		$image = $image[0];		
		$file = basename($_FILES['image']['name']);	
		$temp_path = $_FILES['image']['tmp_name'];
		$target_path = $table . "/" . $image;
		move_uploaded_file($temp_path, $target_path);
	}
	elseif(isset($_GET['item'])) {
		echo "hi";
		$image = $_GET['item'];
	}
	else {
		$image = "";
	}
	return $image;
}

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

(isset($_FILES['image']) && !empty($_FILES['image']) )

and in your form you'll need:
<form enctype="multipart/form-data" method="post" ...>

AND also a file field:
<input type="file" name="image" />
Avatar of FairyBusiness

ASKER

I believe I do have that:

http://www.auroriella.com/edit_product.txt

And this is my latest upload function:

function upload() {
	global $errors;
	if((isset($_FILES['image'])) && (!empty($_FILES['image']))) {
		var_dump($_FILES['image']);
		if(isset($_GET['edit'])) {
			$table = $_GET['edit'];
		}
		if(isset($_POST['table'])) {
			$table = mysql_clean_strings($_POST['table']);
		}
		echo "hello";
		$image = explode('.',basename($_FILES['image']['name']));
		$image = $image[0];		
		$file = basename($_FILES['image']['name']);	
		$temp_path = $_FILES['image']['tmp_name'];
		$target_path = $table . "/" . $image;
		move_uploaded_file($temp_path, $target_path);
	}
	elseif(isset($_GET['item'])) {
		echo "hi";
		$image = $_GET['item'];
	}
	else {
		$image = "";
	}
	return $image;
}

Open in new window

If i'm not uploading anything I want it to go to this area:

elseif(isset($_GET['item'])) {
            echo "hi";
            $image = $_GET['item'];
      }


but it never makes it there except for when I click to go to that page.  It starts out there but when I hit submit it goes up to the

if((isset($_FILES['image'])) && (!empty($_FILES['image']))) {

 area  even if I didn't enter a value into the file upload field
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again!