[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-05-03
5
Medium Priority
?
215 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:FairyBusiness
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 35530963
(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" />
0
 

Author Comment

by:FairyBusiness
ID: 35543437
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

0
 

Author Comment

by:FairyBusiness
ID: 35555160
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
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 35558519
try:
if(  isset($_FILES['image']['name'])  &&  !empty($_FILES['image']['name']) )
0
 

Author Closing Comment

by:FairyBusiness
ID: 35560861
Thanks again!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month20 days, 1 hour left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question