Jump to content

Image upload help


biscoe916

Recommended Posts

Ok so i made a little upload image app, and it works great if there isn't already a picture in the database, otherwise it will just keep putting the same picture into the database over and over, regardless of what picture i choose from my harddrive......

For example:

The table is empty with no pictures in it, so i upload  image1.jpg.  My program then encodes it then stores it into the database.

Then I upload image2.jpg it seems to work ok but when i check the database there are just two instances of the first image. This keep going on and on no matter how many different pictures i upload... Just copies the first one over and over.

 

 

Here is my code:

 

 

<?php
include("auth.php");
// uploadimg.php               
// By Tyler Biscoe             
// 09 Mar 2008                 
// Test file for image uploades
include("connect.php");
include("include/header.php");
$max_file_size = 786432;
$max_kb = $max_file_size/1024;

if($_POST["imgsubmit"])
{
if($_FILES["file"]["size"] > $max_file_size)
	{
		$error = "Error: File size must be under ". $max_kb . " kb.";
	}

if (!($_FILES["file"]["type"] == "image/gif") && !($_FILES["file"]["type"] == "image/jpeg") && !($_FILES["file"]["type"] == "image/pjpeg"))
	{
		$error .= "Error: Invalid file type. Use gif or jpg files only.";

	}

if(!$error)
	{
		echo "<div id='alertBox'> Image has been successfully uploaded! </div>";
		$handle = fopen($_FILES["file"]["tmp_name"],'r');
		$file_content = fread($handle,$_FILES["file"]["size"]);
		fclose($handle);
		$encoded = chunk_split(base64_encode($file_content)); 
		$id = $_POST["userid"];
		echo $_FILES["file"]["tmp_name"];
		$default_exist_sql = "SELECT * FROM members WHERE id='".$id."'";
		$default_result = mysql_query($default_exist_sql);
		$results = mysql_fetch_array($default_result);
		if(!$results["default_image"])
		{
			$insert_sql = "UPDATE members SET default_image = '$encoded' WHERE id='". $id ."'";
			mysql_query($insert_sql);

		}

		$sql = "INSERT INTO images (userid, sixfourdata) VALUES ('$id','$encoded')";
		mysql_query($sql);
	}
	else
	{
		echo "<div id='alertBox'>". $error . "</div>";
	}  
  	}
?>
<br />
<font class="heading"> Upload images </font>
<br /><br />

<form enctype = "multipart/form-data" action = "<?php $_SERVER['PHP_SELF']; ?>" method = "post" name = "uploadImage">
<input type = "hidden" name="userid" value = "<?php echo $_GET["userid"]; ?>" >
<input id="stextBox" type="file" name="file" size="35"><br />
<input type="submit" name="imgsubmit" value="Upload">
</form>
<?php include("include/footer.php"); ?>

 

So frustrating!!

 

 

Link to comment
Share on other sites

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.