1

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!

user3327442
  • 41
  • 1
  • 10
  • Well first of all, what does {$row['data']} print? – berentrom Feb 19 '14 at 11:56
  • the whole page just gets filled with symbols like: �PNG IHDR;����bKGD������� IDATx���y� – user3327442 Feb 19 '14 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 '14 at 11:59

3 Answers3

1

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.

bear
  • 11,364
  • 26
  • 77
  • 129
  • @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 '14 at 12:03
1

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

Community
  • 1
  • 1
Romain
  • 51
  • 3
0

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'].'" />';
Sherin Jose
  • 2,508
  • 3
  • 18
  • 39