If(isset) with $_FILES not working correctly

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
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

If(isset) with $_FILES not working correctly

Post by ianhull »

hi Guys,

I have this

Code: Select all


if(isset($_FILES['logo']['tmp_name'])){
	$uploadedfile = $_FILES['logo']['tmp_name'];
	$src = imagecreatefromjpeg($uploadedfile);
	
	list($width,$height)=getimagesize($uploadedfile);
	move_uploaded_file($uploadedfile, "../logos/". strtolower(str_replace(" ", "-", $company_name. ' ' . $company_site.'_original.jpg')));
	
	$newwidth=30;
	$newheight=($height/$width)*30;
	$tmp=imagecreatetruecolor($newwidth,$newheight);
	
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
	
	$filename = "../logos/". strtolower(str_replace(" ", "-", $company_name. ' ' . $company_site.'.jpg'));
	$logo = strtolower(str_replace(" ", "-", $company_name. ' ' . $company_site.'.jpg'));
	
	imagejpeg($tmp,$filename,100);
	
	imagedestroy($src);
	imagedestroy($tmp); 
	} else {	
	$logo = $_POST['original_logo'];
	};

but for some reason when I do not upload a new logo the code in the if part that is not meant to be executed is.

any ideas why?

on my form I have 1 hidden field call original_logo and another file field called logo

Thanks in advance.

thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

echo '<pre>_FILES: '; var_export($_FILES); echo "</pre>\n";

if(isset($_FILES['logo']['tmp_name'])){
and take a look at the output.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks volka,

I got this returned

Code: Select all



_FILES: array (
  'logo' => 
  array (
    'name' => '',
    'type' => '',
    'tmp_name' => '',
    'error' => 4,
    'size' => 0,
  ),
)


How would I now setup the if/else to determine if the error happened?

Thanks in advance.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

i now managed to do it with

Code: Select all


if($_FILES['logo']['error'] == '4'){
	$logo = $_POST['original_logo'];
	} else {	
	$uploadedfile = $_FILES['logo']['tmp_name'];
	$src = imagecreatefromjpeg($uploadedfile);
	
	list($width,$height)=getimagesize($uploadedfile);
	move_uploaded_file($uploadedfile, "../logos/". strtolower(str_replace(" ", "-", $company_name. ' ' . $company_site.'_original.jpg')));
	
	$newwidth=30;
	$newheight=($height/$width)*30;
	$tmp=imagecreatetruecolor($newwidth,$newheight);
	
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
	
	$filename = "../logos/". strtolower(str_replace(" ", "-", $company_name. ' ' . $company_site.'.jpg'));
	$logo = strtolower(str_replace(" ", "-", $company_name. ' ' . $company_site.'.jpg'));
	
	imagejpeg($tmp,$filename,100);
	
	imagedestroy($src);
	imagedestroy($tmp);
	};

thanks for your help.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You may want to have the file process only if it's error flag is zero.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks for the tip feyd,

I have another issue,

This query does not seem to work with the WHEre clause, any one know why?

Thanks

Code: Select all


$result = mysql_query("SELECT count(*) FROM users WHERE company_name = '$company_name' AND company_site = '$company_site'");

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'd imagine that either the company_name or company_site column doesn't exist. Does it give you the error starting at the WHERE, or after it?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

echo the query
and add error handling to your script, minimal version: mysql_query($query) or die(mysql_error());
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

The coumn definatley exists and the query works if do not use the WHERE clause.

There is no error returned if I error check the file

im confused.

Any ideas anyone?
Thanks
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

ianhull wrote:Any ideas anyone?
volka wrote:mysql_query($query) or die(mysql_error());
Tell us the exact error. It'll show you exactly what part of your WHERE clause is incorrect.
Post Reply