2

I understand the debate over whether to store images in the database but I'm stuck with this design for now. I also know that this topic has been covered a thousand times in terms of pulling the MySQL BLOB into a separate php file for display purposes. My question is a little more narrow and I haven't seen it anywhere.

I am looping image results from my database into an HTML table that shows the file description and upload date. All I want is the file description hyperlinked so when my users click on it the actual image is displayed. I have this working fine in another script where they choose the image from a dropdown and it POSTS the information to another script. For some reason I am wrapped around the axle on how to display the image from a simple hyperlink. I am trying to do this but it displays all the BLOB data on the page. Can someone point me in the right direction on this one?

while ($row = mysql_fetch_array($answer1)) {
    echo '<tr>';
    echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='$row[imageven_id]'></td>";
    echo "<td><a href='$row[file_data]')'>$row[upload_name]</a></td>";
    echo "<td>$row[image_category]</td>";
    echo "<td>$row[upload_date]</td>";
    echo '</tr>';
}
2
  • can you post a sample row of your mysql result? Commented Mar 18, 2012 at 16:52
  • Thanks guys, that was an easy fix. For anyone else looking for the solution, change: codeecho "<td><a href='$row[file_data]')'>$row[upload_name]</a></td>"; To: echo "<td><a href='../php/viewimages.php?id=$row[image_id]')'>$row[upload_name]</a></td>"; Commented Mar 18, 2012 at 17:03

4 Answers 4

3

Storing raw blob data is very complicated if you don't have image type stored somewhere. I hope you have either fixed the type of image the system accepts or have stored the image type somewhere in the table too.

Any ways, when you showing images using blob data in a browser, you need to know the image type. Create a separate page to show images only. For example create showimage.php. On this page write the following code

$id= abs($_GET['id']);
$query = mysql_query("SELECT file_data FROM imagetable WHERE id='$id'");
$data=mysql_fetch_array($query);

header('Content-type: image/jpg'); //This is where we need to know the image type
echo $data['file_data'];

Now, you can link to this page to show the image, in this way

echo "<a href='showimage.php?id=".$row['id']."'>".$row['upload_name']."</a>";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Starx that was exactly the answer I went with. Perfect explanation.
1

If you know the type of the image, you can use the data URI scheme:

function data_uri($contents, $mime) {
    $base64 = base64_encode($contents);
    return "data:$mime;base64,$base64";
}

echo sprintf(
    '<img src="%s" alt="%s" />',
    data_uri($row['file_data'], 'image/png'),
    $row['upload_name']
);

1 Comment

+1 I was going to also suggest a data URI, but if the image was to be a separate HTTP request, might as well directly dump.
1
<?php
header("Content-Type: image/jpeg"); // we'll assume

// pass the row ID based upon your example, this appears to be it.
$imageven_id = $_GET["imageven_id"]

// do the same query you did above, then
// keep in mind this variable is in memory, hope your images are
// not too large.
print $row['file_data'];
?>

Comments

0

$row[file_data] is going to be the raw data of the file itself, not a link to a script that would display that data. Fix that. :)

2 Comments

Okay, so the idea is I am still linking to that outside script. Maybe something like :
echo "<td><a href='abc.php?id=1')'>$row[upload_name]</a></td>"; to transfer the _GET of the image ID?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.