Jump to content

Error in image file upload script - HELP!


barbs75

Recommended Posts

Hi guys,

 

Im having trouble with my script to process my image file upload. My script worked fine on my localserver, however, now that i have loaded it onto my web hosting server, and i run the script, i get the following error:

 

Parse error: syntax error, unexpected '{' in /homepages/43/d149598129/htdocs/go4home/upload_process.php on line 22

 

If you see my code below, you can see where the error is being picked up:

 

upload_process.php

<?php
//starts session and checks loggin status 
session_start(); 
header("Cache-control: private"); 
if (!$_SESSION['username']) { 
    include("login_form.php"); 
    exit(); 
} 

//create error array
$errorarray = array();
$maxsize = $_POST['MAX_FILE_SIZE'];
$user_id = $_SESSION['user_id'];

//check if a file has been uploaded at all
if(!isset($_FILES['userfile']))//if file has not be submitted
{
$errorarray['file_entry'] = "Please select a file";//place error message into file_entry array element
}
else
{
try { <!--------ERROR BEING PICKED UP HERE!!!!!!!!!
	upload($maxsize);
}//try upload function		

catch(Exception $e)
{
	$error = 'Error: '. $e->getMessage().'<br>';
	$_SESSION['upload_error'] = $error;
	header('Location:upload.php');
}
}

// the upload function
function upload($maxsize){

if(is_uploaded_file($_FILES['userfile']['tmp_name']))//if a file was uploaded and is given a temporary name 
{
	//set max height and max width of image
	$max_height = 300;
	$max_width  = 400;
	$user_id = $_SESSION['user_id'];	

	// check the file is less than the maximum file size
	if($_FILES['userfile']['size'] > $maxsize)
	{
		throw new Exception('Sorry, the image you tried to upload was too large',42);
	}	

	// get the image size info..
	$info = getimagesize($_FILES['userfile']['tmp_name']);

	//check the extension.
	$array = explode(".", $_FILES['userfile']['name']);//splits the filename at the '.' to collect fparts of filename
	$nr    = count($array);//nr is the result of the number of elements of the array 
	$ext   = $array[$nr-1];//ext collects the last element of array, which should be extension
	//if the extension is neither of the extensions given, throw out exception
	if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png"))
	{ 
		throw new Exception('File extension un-recognized. Be sure your image follows the correct extension (.JPG or .PNG)', 42);
	}
	//if extension is accepted carry on with script.....

	//checking and double checking filetype
	//CHECK TYPE: (what the browser sent)
	if(($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile'][					'type'] != "image/png")) 
	{
		throw new Exception('Upload file type un-recognized. Only .JPG or .PNG images allowed.',43);
	}


	//DOUBLE CHECK TYPE: if image MIME type from GD getimagesize() -In case it was a FAKE!							
	if(($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) 
	{
		throw new Exception('Upload file type un-recognized. Only .JPG or .PNG images allowed.',44);
	}

	//check image size(height & width)
	if(($info[0] > $max_width) || ($info[1] >$max_height)) 
	{
    		echo "<BR><BR>Error: Image size error (<b>" . $info[0] . "</b> x <b>" . $info[1] . "</b>). Must not exceed ". $max_height . " x ". $max_width .".";
		throw new Exception('You have an Image Size error ',45);
	}
	//image is fine and can continue through script
	require_once('database.php');

	//rename file, move it to location.
	if(is_uploaded_file($_FILES['userfile']['tmp_name'])) 
	{
		//get max number of images the user has uploaded 
		$m = mysql_query("SELECT max(user_images) as `total_images` FROM `images` WHERE `user_id` = '$user_id'");
			 if(!$m) die('An Error Occurred.');
			   $result = mysql_fetch_object($m);
				  if($result->total_images <= 0) {
						$image_number = 1;
				  } else {
				 $image_number = $result->total_images + 1;
				  } //end if

		$filename = strtolower($_SESSION['username']) . $image_number;

		  if(move_uploaded_file($_FILES['userfile']['tmp_name'] , $_SERVER['DOCUMENT_ROOT']."/uploads/images/".$filename . '.' . $ext)) {
				  //send all of the data of the image into the database
				$sql="INSERT INTO images (user_id,image_name,user_images) VALUES ('$user_id','$filename','$image_number')"; 

				if (!mysql_query($sql,$con)) 
				{
					die('Error: ' . mysql_error()); 
				} 
				mysql_close($con);
				include('upload_success.php');
		   } else {
				throw new Exception('Sorry, your file has not been uploaded successfully',46);
		   }//end upload
		} //end is_uploaded_file		
}
else 
{
	 throw new Exception('Sorry, you havent uploaded an image',47);
	  //'<div>File exceeds the Maximum File limit</div>
	  //<div>Maximum File limit is '.$maxsize.'</div>
	  //<div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
	 // <hr />';
 }
}//end of function upload()

?>

 

Can anyone see where the error is? because i sure as hell can't, don't understand why it would be tripping up like this

 

cheers

 

Craig

Link to comment
https://forums.phpfreaks.com/topic/101378-error-in-image-file-upload-script-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.