Retrieving a Binary Image from MySQL and Displaying it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raptor354
Forum Newbie
Posts: 13
Joined: Tue Jun 16, 2009 3:21 pm

Retrieving a Binary Image from MySQL and Displaying it

Post by raptor354 »

I've been trying to make this work for about 1 1/2 days now without complete success.

I'm trying to make a web site (written primarily in HTML, but lots of PHP involved) in which a user would log in, click an "Upload Image" link, upload an image that gets stored in binary inside a MySQL database, then that image is displayed when the user returns to their post-log-in page.

Here's what I have so far for storing the picture in the database.

Code: Select all

addslashes(fread(fopen($_FILES['uploadedfile']['tmp_name'], "r"), filesize($_FILES['uploadedfile']['tmp_name'])));
As you can tell, the code gets data from the form (on the previous HTML page) and formats the data so that it can't hurt MySQL. Of course, there is an INSERT statement later on, and the whole system up to this point works. When read straight from the database using a management tool and compared to the original image, it is exactly the same thing.

My problem comes when I try to retrieve that image. I have no idea how to do it! Ideally, there would be a javascript slideshow of all the pictures they've uploaded, but that will get asked in a different forum or I'll just learn javascript.

So far, I've been able to download a file (whose name is "download.php") that, once renamed, displays the picture correctly by calling this:

Code: Select all

<a href='download.php?SubmissionId=$data->SubmissionId'>Download</a>
download.php

Code: Select all

$subid = $_GET['SubmissionId'];
$sql = "SELECT SubmissionTitle,Picture FROM Submission WHERE SubmissionId='$subid'";
$result = mysql_query($sql);    
$data = mysql_result($result, 0, "Picture");
header("Content-type:'image/jpg'");
echo $data;
Even me reading my own question confuses me. If anybody needs more specific details or clarification, just ask.

Much appreciation!

Raptor354

P.S.: I've tried several examples off the Internet, but to no avail
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Retrieving a Binary Image from MySQL and Displaying it

Post by requinix »

raptor354 wrote:My problem comes when I try to retrieve that image...

So far, I've been able to download a file (whose name is "download.php") that, once renamed, displays the picture correctly
I'm not sure what you're asking for. You want to let the user actually download the image? That won't work when you get your JavaScript thing working.
raptor354
Forum Newbie
Posts: 13
Joined: Tue Jun 16, 2009 3:21 pm

Re: Retrieving a Binary Image from MySQL and Displaying it

Post by raptor354 »

I just want to display the image in an imagebox.

Raptor354
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Retrieving a Binary Image from MySQL and Displaying it

Post by McInfo »

Instead of using a hyperlink, use an <img> tag.

Code: Select all

<img src="download.php?SubmissionId=3" alt="" />
Related example: http://misterchucker.com/image_blob_test/

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:19 pm, edited 1 time in total.
raptor354
Forum Newbie
Posts: 13
Joined: Tue Jun 16, 2009 3:21 pm

Re: Retrieving a Binary Image from MySQL and Displaying it

Post by raptor354 »

McInfo wrote:Instead of using a hyperlink, use an <img> tag.

Code: Select all

<img src="download.php?SubmissionId=3" alt="" />
I was using a hyperlink as a testbench, although the imagebox worked wonderfully.

All is right with the world again, after a quick read through the code on the website McInfo specified. The website shows a different way of displaying the picture (using imagecreatefromstring() and imagejpeg()) than I expected.

Thanks again!

Raptor354
Post Reply