Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Upload Script - Undefined Variable Error 1

Status
Not open for further replies.

rexolio2008

Technical User
Apr 24, 2008
41
US
I think this will be easy - I just don't know much about PHP to figure it out.

I paid someone to build this PHP upload script for me. Now I can't get in touch with them to fix it.

The script below successfully uploads an image that is over 400 pixels in height or width, but if the image is less than that, I get an error on line 45 that nwidth and nheight is undefined (I marked line 45 below).

Help??

CONFIG FILE:
Code:
<?php
require_once 'functions.php';
$save_into_db = false;
$server_name = 'servername-here'; 
$user = 'dbusername-here';
$password = 'dbpassword-here'; 
$db_name = 'dbname-here';
if($save_into_db)
{
	mssql_connect($server_name, $user, $password);
	mssql_select_db($db_name);
	init();
}
$file_path =dirname(__FILE__);
$upload_to = $file_path.'\\upload\\'; 
$num_files = 1;
$resize_to = 400;
$quality = 90;
?>

UPLOAD SCRIPT:
Code:
<?php
require_once 'config.php';    

if($_SERVER['REQUEST_METHOD'] == 'POST') {
	$error = array();
	$message = array();
	if($_FILES['image']) 
        {
            if(is_array($_FILES['image']['name']) && count($_FILES['image']['name']))
            {
            	foreach ($_FILES['image']['name'] as $key=>$name)
	        	{
	            	if(!$_FILES['image']['name'][$key])
	            		continue;
	            	if(file_exists($upload_to.$_FILES['image']['name'][$key]))
	            	{
		             	$error[] = 'File '.$_FILES['image']['name'][$key].' already exists';
		             	continue;
	            	}
	        		preg_match('/\.([A-Za-z]+?)$/', $_FILES['image']['name'][$key], $matches);
		            $matches[1] = strtolower($matches[1]);
		            if($matches[1] == 'png' && function_exists('imagecreatefrompng') || $matches[1] == 'jpg' && function_exists('imagecreatefromjpeg') || $matches[1] == 'jpeg' && function_exists('imagecreatefromjpeg') || $matches[1] == 'gif' && function_exists('imagecreatefromgif')) {
		                list($owidth, $oheight) = getimagesize($_FILES['image']['tmp_name'][$key]);
		                if($matches[1] == 'png')
		                    $original = imagecreatefrompng($_FILES['image']['tmp_name'][$key]);
		                if($matches[1] == 'jpg' || $matches[1] == 'jpeg')
		                    $original = imagecreatefromjpeg($_FILES['image']['tmp_name'][$key]);
		                if($matches[1] == 'gif')
		                    $original = imagecreatefromgif($_FILES['image']['tmp_name'][$key]);
			            
		                if((($oheight>$resize_to) && ($oheight>$owidth)) || (($oheight>$resize_to) && ($oheight==$owidth))) {
			            	$nheight = $resize_to;
		                    $nwidth = $nheight / $oheight * $owidth;
		                    $resized = imagecreatetruecolor($nwidth, $nheight);
		                }
		                elseif(($owidth>$resize_to) && ($owidth>$oheight)) {
		                	$nwidth = $resize_to;
		                    $nheight = $nwidth / $owidth * $oheight;
		                    $resized = imagecreatetruecolor($nwidth, $nheight);
		                }
		                else
		                {
		                	$resized = $original;
		                }
LINE 45 HERE            imagecopyresampled($resized, $original, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight);
		                switch ($matches[1]) {
		                	case 'png':
		                		imagepng($resized,$upload_to.$_FILES['image']['name'][$key],$quality);
		                	break;
		                	case 'jpg':
		                		imagejpeg($resized,$upload_to.$_FILES['image']['name'][$key],$quality);
		                	break;
		                	case 'gif':
		                		imagegif($resized,$upload_to.$_FILES['image']['name'][$key],$quality);
		                	break;
		                	default:
		                		$error[] = 'Unsupported image type for file: '.$_FILES['image']['name'][$key];	
		                	break;
		                }
		            	if(file_exists($upload_to.$_FILES['image']['name'][$key]))
		                {
		                	$message[] = 'File: '.$_FILES['image']['name'][$key].' successfully uploaded.';
		                	if($save_into_db)
		                	{
								$data = array('name'=>$_FILES['image']['name'][$key],'size'=>$_FILES['image']['size'][$key],'type'=>$matches[1]);
			                	save_file($data);	
		                	}
		                }
		            } 
		            else
		            {
		            	$error[] = 'Unsupported image type for file: '.$_FILES['image']['name'][$key];
		            }

	        	}
	        	//mssql_close();
          	}
          	else
          	{
          		$error[] = 'No images uploaded!';
          	}
        } else
        {
        	$error[] = 'No images recieved';
        }
            
    }
?>
 
You don't define your nheight and nwidth variables when the image is not larger than 400px. So of course they are not defined when you try to use them.


Code:
[red]                 if((($oheight>$resize_to) && ($oheight>$owidth)) || (($oheight>$resize_to) && ($oheight==$owidth))) {
                            $nheight = $resize_to;
                            $nwidth = $nheight / $oheight * $owidth;
                            $resized = imagecreatetruecolor($nwidth, $nheight);
                        }
                        elseif(($owidth>$resize_to) && ($owidth>$oheight)) {
                            $nwidth = $resize_to;
                            $nheight = $nwidth / $owidth * $oheight;
                            $resized = imagecreatetruecolor($nwidth, $nheight);
                        }[/red]
                       [blue] else
                        {
                            $resized = $original;
                        }
[/blue]


The red section does all the checking to see if either the height of the image or the width of the image is more than 400. However in the event neither of them are, then your variables just aren't defined, Yet you still call your the imagecopyresample function to resize it. But since your variables aren't defined it throws the error.

The question here is do you want to call the function even though the image size is smaller than 400 or do you just don't want to do anything.

Depending on that will be what to code.

The simplest option, would be to just define nwidth and nheight in the last else statement so they are available to the function. But I don't see much point in calling a function if you don't really want it to do anything.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Vacunita... Thanks for the quick respose. Doesn't make sense to call the function if it's not used. I just don't know how to change it. Everything I've tried doesn't work.
 
Try something simple like:

Code:
[red]else
                        {
                            $resized = $original;
                        }
[/red]
[blue]if(isset($nheight) AND isset($nwidth)){
       [green]    imagecopyresampled($resized, $original, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight);[/green]
}
[/blue]
[red]
                        switch ($matches[1]) {
                            case 'png':
                                imagepng($resized,$upload_to.$_FILES['image']['name'][$key],$quality);


[/red]

This essentially checks whether or not $nheight and $nwidth are actually set, if they are it goes on and resizes the image, otherwise it just moves on.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Glad I could help.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Anyone know how I can change the name with the script above? I'd like to be able to remove spaces and special characters from the name... And perhaps prepend a unique identifier to the name.
 
How are you saving your file?

I'd assume you can change the name when you are saving it. but the only thing I see that would maybe do that is a function called save_file(). Probably something in there should let you change the name. But without knowing what it does its hard to provide any help.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top