Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I've made a php-page where you can upload images/files that will be saved in my database as the type "mediumblob". Now I'm stuck and can't figure out how to display these images from the database. I wrote in caps where I would like to print out the images.

<?php
$dbLink = new mysqli('127.0.0.1', 'root', '', 'test');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}

$sql = 'SELECT `id`, `name`, `mime`, `size`, `created`, `data` FROM `file`';
$result = $dbLink->query($sql);

if($result) {
// Kolla om det finns några filer i databasen
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {

echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>Mime/type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b>Image</b></td>
</tr>';

while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
//THIS IS WHERE I'M SUPPOSED TO PRINT OUT THE IMAGES
</tr>";
}



echo '</table>';
}
}

else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
echo '<p>Click <a href="index.html">here</a> to go back</p>';
// Close the mysql connection
$dbLink->close();
?>

This is my table in the database:

CREATE TABLE `file` (
`id` Int Unsigned Not Null Auto_Increment,
`name` VarChar(255) Not Null Default 'Untitled.txt',
`mime` VarChar(50) Not Null Default 'text/plain',
`size` BigInt Unsigned Not Null Default 0,
`data` MediumBlob Not Null,
`created` DateTime Not Null,
PRIMARY KEY (`id`)
) 

Very thankful for any help!

share|improve this question

marked as duplicate by david strachan, kuroi neko, M Khalid Junaid, slm, Mat Nadrofsky Feb 27 at 14:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Well first of all, what does {$row['data']} print? –  berentrom Feb 19 at 11:56
    
the whole page just gets filled with symbols like: �PNG IHDR;����bKGD������� IDATx���y� –  user3327442 Feb 19 at 11:58
1  
i advise to avoid the image/file stored in db.try the upload and stored in folders.now you think the image/file size is small,but in future the db size will extremely high....... –  Raj Mohan Feb 19 at 11:59

3 Answers 3

If the BLOB contains the binary data of an image , so you'd need to take the content and potentially write it to file, resulting in an image.

 file_put_contents($path."path/to/image/".$row['name'], base64_decode($row['image']));

Then call it normally. Of course.. check that the file exists before writing it :)

Alternatively, you could write a script to directly output the image as shown below.

share|improve this answer
    
@BillyBigPotatoes the post already addresses this, not to mention, for performance purposes, do you always want to be loading the image from the database or cache it? –  bear Feb 19 at 12:03

There is two approach for doing this:

You could use inline base64 encoding. You could do it this way assuming your images are jpeg:

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

The next option would be to create a PHP file called image.php that takes the ID of the image as parameter and then output the image. Your HTML would then look like this:

<td><img src="image.php?id={$row['id']}"></td>

Anyway, there is numerous of answered topics on this matter. You may have a look at this one for example: I need my PHP page to show my BLOB image from mysql database

share|improve this answer

Try this;

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header('Content-length: '.$row['size']);
header('Content-Type: '.$row['mime']);
header('Content-Disposition: attachment; filename='.$row['name']);
echo '<img src="'.$row['data'].'" />';
share|improve this answer
    
Why the img tag? –  EJTH Feb 19 at 12:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.