Image Upload form Issues
Moderator: General Moderators
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
Image Upload form Issues
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
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
Last edited by the9ulaire on Mon Jun 11, 2007 11:45 am, edited 1 time in total.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
feyd | Please use
-------------------------------------------------------
Here's showimage.php:
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]- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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
}-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am