5 Replies - 3250 Views - Last Post: 16 March 2013 - 03:39 PM Rate Topic: -----

#1 ilinan87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-April 10

Problem with uploading image into mysql database then displaying it

Posted 16 March 2013 - 11:15 AM

Having a hard time inserting images into a mysql db, into blob type. Then retrieving the image and displaying it.

I've tried several approaches to displaying it, and all different attempts have lead me to this code.

Still only shows me broken image icon.

Here is code to upload image


<form enctype="multipart/form-data" action="loggedin.php" method="post" name="changer">
Photo name: <input type="text" name="photoName"><br>
<textarea name="caption" cols="25" rows="5">
Enter your caption here...
</textarea><br>
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" accept = "image/jpg"accept "image/png" type="file">
<input value="Upload" type="submit">

<?php
//check to see if the user where sending to, exists
if(isset($_FILES['image']) && $_FILES['image']['size'] > 0 && isset($_POST['photoName']))
	{
		//temporary file name
		// Temporary file name stored on the server
		$tmpName = $_FILES['image']['tmp_name'];
		$imageType = $_FILES['image']['type'];

		// Read the file
		$fp = fopen($tmpName, 'r');
		$data = fread($fp, filesize($tmpName));
		//$data = addslashes($data);
		fclose($fp);

		// this is where we check to see if the user exists in the database
		//create sql string to retrieve the string from the database table "users"
		$sql="INSERT INTO photos (photoName, caption, photoData, photoType, userName)
			VALUES
			('$_POST[photoName]','$_POST[caption]','$tmpName','$imageType', '$currentUser')";

		//For debugging purposes
		if(!mysqli_query($con,$sql))
		{
			die('Error: ' . mysqli_error($con));
		}
		else
		{
			echo "Your Image has been Added";
		}	
	}


here is display image code

if(isset($_POST['usersImage'])){
			//code to show images
			$user = $_POST['usersImage'];
			$sql = "SELECT * FROM `photos` WHERE userName = '$user'";
			$result = mysqli_query($con,$sql);
			while($row = mysqli_fetch_array($result)) 
			{
				switch ($row['photoType']) {
					case 'image/jpeg':
					//echo "<tr>";  
					//echo '<img src="data:image/jpeg;base64,'. base64_encode($row['photoData'])."\"></td>";   
					//echo "</tr>"; 
						echo "<br>";
						echo '<img src=data:image/jpeg>' .$row['photoData']. '.jpeg'.'</p>';
						echo '<p id="caption">'.$row['caption'].' </p>';
						
					break;
				}			
			}
		}



The commented out echos are the different attempts I've tried to solving my problem

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with uploading image into mysql database then displaying it

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3807
  • View blog
  • Posts: 13,500
  • Joined: 12-December 12

Re: Problem with uploading image into mysql database then displaying it

Posted 16 March 2013 - 11:37 AM

You are reading the image as $data but inserting just $tmpName into the database. Here is some sample code that uses file_get_contents():

# create table blobs( id int(11) NOT NULL auto_increment, myblob longblob, PRIMARY KEY (id));


$host     = "localhost";
$username = "blobtest";
$password = "blobtest";
$database = "blobtest";

$link_id = @mysql_connect($host, $username, $password);
@mysql_select_db($database);

$blob = file_get_contents('stripe.png');
$bdata = addslashes($blob);

$qry = "INSERT INTO blobs VALUES(NULL, '$bdata')";
mysql_query($qry);

SO reference

Concentrate on storing it correctly firstly, then you can turn to the issue of displaying the image. I would guess that you could use PHPMyAdmin (or similar) to double-click the image to check that it has been stored in your database.

Many suggest that it is preferable to store just the image filename and location, storing the image in a server-folder, and then use this as the src for the img. but it is certainly possible to store the image itself.
Was This Post Helpful? 0
  • +
  • -

#3 ilinan87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-April 10

Re: Problem with uploading image into mysql database then displaying it

Posted 16 March 2013 - 12:55 PM

i changed the stuff to correspond to the example you gave me, now its uploading correcctly. As I am able to directly download the image I upload from my phpmyadmin, and its viewable on my local machine.

But stil doesn't print correctly from browser.

I tried this to print, but it gives me a bunch of weird data

$contents = stripslashes($row['photoData']);
				echo '<img src=data:image/jpeg>' .$contents . '.jpeg'.'</p>';
				print $contents;

This post has been edited by Dormilich: 16 March 2013 - 05:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3807
  • View blog
  • Posts: 13,500
  • Joined: 12-December 12

Re: Problem with uploading image into mysql database then displaying it

Posted 16 March 2013 - 02:04 PM

Your src-format is incomplete. Try:

echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['photoData'] ) . '" />';

that is, without the stripslashes firstly. And remove the print statement.

Also, I suspect that data-URIs don't work in every browser. Test it in different browsers.

Quote

I tried this to print, but it gives me a bunch of weird data

that is from the (unnecessary) print statement :whatsthat:
Was This Post Helpful? 1
  • +
  • -

#5 ilinan87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-April 10

Re: Problem with uploading image into mysql database then displaying it

Posted 16 March 2013 - 03:30 PM

Working correctly now, I guess only problem I have now, is that on a certain size, the image wont upload anymore.
But for smaller sizes, it works fine. Not sure, what the limit of size is

This post has been edited by Dormilich: 16 March 2013 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3807
  • View blog
  • Posts: 13,500
  • Joined: 12-December 12

Re: Problem with uploading image into mysql database then displaying it

Posted 16 March 2013 - 03:39 PM

The MAX_FILE_SIZE attribute is in bytes so you might increase the value.

MAX_FILE_SIZE should not be relied upon and you should separately set, or change, the maximum file-size limit on the server-side, probably in the php.ini file.

This post has been edited by andrewsw: 16 March 2013 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1