Page 1 of 1

Image Upload form Issues

Posted: Mon Jun 11, 2007 11:37 am
by the9ulaire
I'm working through a book right now on PHP, MySQL, and Web Dev. Every now and then I've run into problems but have been able to figure out all of them so far. Today I've been following along creating a Image Upload.

Here's the directory of the files I'm working with:
http://lwrussell.com/phptesting/chapt7/

Here's my upload form:
http://lwrussell.com/phptesting/chapt7/upload_image.htm

I think my book has omitted some details because I cannot figure out the issue. I downloaded the codes for the books and tried using those too but they didn't work. I believe the book is telling me to just go to: http://lwrussell.com/phptesting/chapt7/showimage.php but it's not entirely clear.

Thanks any help!
Luke

Posted: Mon Jun 11, 2007 11:43 am
by superdezign
Well, we can't figure out the errors through your example (I don't think.. didn't check). You need to give us the code where you think the error is occurring.

Posted: Mon Jun 11, 2007 11:43 am
by volka
Parse error: syntax error, unexpected $end in /home/.grasshoppah/lucious/lwrussell.com/phptesting/chapt7/check_image.php on line 73
You might want to show us the code?

Posted: Mon Jun 11, 2007 11:51 am
by the9ulaire
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Sorry, I forgot to put in the code (I should know better by now).

Here's check_image.php:

Code: Select all

<?php
//connect to the database
$link = mysql_connect("hostomitted", "usernameomitted", "passwordomitted")
	or die("Could not connect: " . mysql_error());
mysql_select_db('moviesite', $link)
	or die(mysql_error());

//make variables available
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");

//upload image and check for image type
$ImageDir ="images/";
$ImageName = $ImageDir . $image_tempname;

if (move_uploaded_file($_FILES['image_filename']['tmp_name'], 
                      $ImageName)) {

  //get info about the image being uploaded
  list($width, $height, $type, $attr) = getimagesize($ImageName);

  //**insert these new lines
  if ($type > 3) {
    echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
         "PNG file.<br>";
    echo "Please hit your browser's 'back' button and try again.";
  } else {

    //image is acceptable; ok to proceed

  //**end of inserted lines

  //insert info into image table

  $insert = "INSERT INTO images
            (image_caption, image_username, image_date)
            VALUES
            ('$image_caption', '$image_username', '$today')";
  $insertresults = mysql_query($insert)
    or die(mysql_error());

  $lastpicid = mysql_insert_id();

  //change the following line:
  $newfilename =  $ImageDir . $lastpicid . ".jpg";

  //**insert these lines
  if ($type == 2) {
    rename($ImageName, $newfilename);
  } else {
    if ($type == 1) {
      $image_old = imagecreatefromgif($ImageName);
    } elseif ($type == 3) {
      $image_old = imagecreatefrompng($ImageName);
    }

    //"convert" the image to jpg
    $image_jpg = imagecreatetruecolor($width, $height);
    imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, 
                     $width, $height, $width, $height);
    imagejpeg($image_jpg, $newfilename);
    imagedestroy($image_old);
    imagedestroy($image_jpg);
  }
  
  $url = "location: showimage.php?id=" . $lastpicid;
  header($url);
  //**end of inserted lines
}

?>
-------------------------------------------------------
Here's showimage.php:

Code: Select all

<?php

//connect to the database
$link = mysql_connect("hostomitted", "usernameomitted", "passwordomitted")
	or die("Could not connect: " . mysql_error());
mysql_select_db('moviesite', $link)
	or die(mysql_error());

//make variables available
$id = $_REQUEST['id'];

//get info on the pic we want
$getpic = mysql_query("SELECT * FROM images WHERE image_id = '$id'")
  or die(mysql_error());
$rows = mysql_fetch_array($getpic);
extract($rows);

$image_filename = "images/" . $image_id . ".jpg";

list($width, $height, $type, $attr) = getimagesize($image_filename);

?>

<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1><br><br>
<p>Here is the picture you just uploaded to our servers:</p>
<img src="<?php echo $image_filename; ?>" align="left" 
  <?php echo $attr; ?> >
<strong><?php echo $image_caption; ?></strong><br>
It is <?php echo $width; ?> pixels wide and <?php echo $height; ?> pixels high.<br>
It was uploaded on <?php echo $image_date; ?> 
by <?php echo $image_username; ?>.
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jun 11, 2007 12:19 pm
by superdezign
What's wrong with it? Where does it fail?

Posted: Mon Jun 11, 2007 12:27 pm
by volka

Code: Select all

if (move_uploaded_file($_FILES['image_filename']['tmp_name'],  $ImageName)) {
	//get info about the image being uploaded
	list($width, $height, $type, $attr) = getimagesize($ImageName);
	//**insert these new lines
	if ($type > 3) {
		echo "Sorry, but the file you uploaded was not a GIF, JPG, or PNG file.<br>";
		echo "Please hit your browser's 'back' button and try again.";
	}
	else {
		//image is acceptable; ok to proceed
		//**end of inserted lines
		//insert info into image table
		$insert = "INSERT INTO images
				(image_caption, image_username, image_date)
			VALUES
				('$image_caption', '$image_username', '$today')
		";
		$insertresults = mysql_query($insert) or die(mysql_error());
		$lastpicid = mysql_insert_id();
		//change the following line:
		$newfilename =  $ImageDir . $lastpicid . ".jpg";

		//**insert these lines
		if ($type == 2) {
			rename($ImageName, $newfilename);
		}
		else {
			if ($type == 1) {
				$image_old = imagecreatefromgif($ImageName);
			}
			elseif ($type == 3) {
				$image_old = imagecreatefrompng($ImageName);
			}

			//"convert" the image to jpg
			$image_jpg = imagecreatetruecolor($width, $height);
			imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width, $height, $width, $height);
			imagejpeg($image_jpg, $newfilename);
			imagedestroy($image_old);
			imagedestroy($image_jpg);
		}
		$url = "location: showimage.php?id=" . $lastpicid;
		header($url);
		//**end of inserted lines
	}
There's a } missing

Posted: Mon Jun 11, 2007 1:04 pm
by the9ulaire
Awesome! Thank you so much!

Since I'm new to PHP, I'm not very good at knowing some of the basic things to look for first. I'll be sure to remember that. Thanks again!

Posted: Mon Jun 11, 2007 1:08 pm
by guitarlvr
Not sure what your using to edit your php files but a syntax highlighter such as eclipse. When the cursor is next to a bracket, it will show you where your closing bracket is. Very helpful.

Wayne

Posted: Mon Jun 11, 2007 1:23 pm
by the9ulaire
I've been using Dreamweaver 8. I'll look into that though. Thanks.

Posted: Mon Jun 11, 2007 2:10 pm
by guitarlvr
dreamweaver does do syntax highlighting for php. I had used dreamweaver MX 2004 which did pretty much everything i needed except the bracket highlights. not sure if dreamweaver 8 does that. Check it out though. :)

Wayne

Posted: Mon Jun 11, 2007 2:36 pm
by the9ulaire
DW8 does do that, and in fact, I love the highlighting for it helps me so much. I guess the highlighting wasn't affected since the } missing was at the very end.