Page 1 of 1

Retrieving a Binary Image from MySQL and Displaying it

Posted: Tue Jun 16, 2009 3:53 pm
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

Re: Retrieving a Binary Image from MySQL and Displaying it

Posted: Tue Jun 16, 2009 5:47 pm
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.

Re: Retrieving a Binary Image from MySQL and Displaying it

Posted: Tue Jun 16, 2009 5:50 pm
by raptor354
I just want to display the image in an imagebox.

Raptor354

Re: Retrieving a Binary Image from MySQL and Displaying it

Posted: Tue Jun 16, 2009 6:13 pm
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.

Re: Retrieving a Binary Image from MySQL and Displaying it

Posted: Wed Jun 17, 2009 1:10 am
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