Jump to content

Recommended Posts

Hi, I'm new to the PHP world. I'm using a book example for an image upload and view and for some reason it's not working. I've done this example before and wasn't sure if I had to make some mods to it to make it work (some book examples had errors). I can't go back to see my old code because I failed to back it up and mutilated it with the following book example (that I couldn’t get to due to the increase in my workload).

Below is the HTML & PHP code I’m using. The main issue is that it’s not writing to the database and saving the file to the directory. I also have the PHP for the DB table creation if needed.

 

HTML:

<html>
<head>
<title>Upload your pic to our site!</title>
</head>
<body>

<form name="form1" method="post" action="check_image.php" enctype="multipart/form-data">

<table border="0" cellpadding="5">
<tr>
  <td>Image Title or Caption<br />
  <em>Example: You talkin' to me?</em></td>
  <td><input name="image_caption" type="text" id="image_caption" size="55" maxlength="255" /></td>
</tr>
<tr>
  <td>Your Username</td>
  <td><input name="image_username" type="text" id="image_username" size="15" maxlength="255" /></td>
</tr>
  <td>Upload Image:</td>
  <td><input name="image_filename" type="file" id="image_filename" /></td>
</tr>
</table>
<br />
<em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em>
<p align="center"><input type="submit" name="Submit" value="Submit" />
 
<input type="reset" name="Submit2" value="Clear Form" />
</p>
</form>
</body>
</html>

 

PHP:

<?php
//connect to database
$link = mysql_connect("localhost", "dir", "pass")
or die("Could not connect: " . mysql_error());
mysql_select_db("moviesite", $link)
or die(mysql_error());

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

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

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

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

switch($type){
	case 1:
		$ext = ".gif";
		break;
	case 2:
		$ext = ".jpg";
		break;
	case 3:
		$ext = ".png";
		break;
	default:
		echo "Sorry, but the file you uploaded was not a GIF, JPG or PNG file.<br>";
		echo "Please hit the BACK button and try again.";
}

//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();

$newfilename = $ImageDir . $lastpicid . $ext;

rename($ImageName, $newfilename);

}

?>

<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="images/<?php echo $lastpicid . $ext; ?>" align="left" />
<strong><?php echo $image_name; ?></strong><br />
This image is a <?php echo $ext; ?> image.<br />
It is <?php echo $width; ?> pixels wide
and <?php $height; ?> pixels high."<br>
It was uploaded on <?php echo $today; ?>.
</body>
</html>

 

Any solutions will be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/
Share on other sites

The main issue is that it’s not writing to the database and saving the file to the directory.

 

So, in turn does not display the image nor the image parameters.

 

I've tested the old files before making the change, and it saved the file and created entries into the DB just fine.

try changing this

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

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

switch($type){
	case 1:
		$ext = ".gif";
		break;
	case 2:
		$ext = ".jpg";
		break;
	case 3:
		$ext = ".png";
		break;
	default:
		echo "Sorry, but the file you uploaded was not a GIF, JPG or PNG file.<br>";
		echo "Please hit the BACK button and try again.";
}

 

to this, to see if there is a problem with moving the file

 

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

 

//get info about the image being uploaded

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

 

switch($type){

case 1:

$ext = ".gif";

break;

case 2:

$ext = ".jpg";

break;

case 3:

$ext = ".png";

break;

default:

echo "Sorry, but the file you uploaded was not a GIF, JPG or PNG file.<br>";

echo "Please hit the BACK button and try again.";

}

else {

echo "FIle upload failed!";

exit();

}

Give me a "parse error". I think it's because of the }else{ is off of the switch() not the if().

 

So, I moved it to the end of the if() and of course when I run it, it states that the "File upload failed!". Which is what I'm trying to get resolved. It did work for the prev file(s) before I went back to review. But for some reason it's not.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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