Hasan Sait Arslan
Hasan Sait Arslan

Reputation: 65

How can I display an image stored in a database on a php page?

I couldn't manage to show retrieved blob image file as image on my php page. The code part that shows the retrieved data from the database is as follows:

echo '<h4>'.$yaz["Header"].'</h4><br>'.$yaz["Picture"].'<p class="text">'.$yaz["Description"].'</p><br>';

when I write this code, I get Header and Description and any problem does not occur. But the picture comes as

����JFIFHH��:>ExifMM*����(2�;���i؈%PdCanonCanon EOS 500DHH2012:09:05 10:23:46 

How can I display my picture on the php page?

Upvotes: 0

Views: 85

Answers (3)

sudhakar
sudhakar

Reputation: 119

Try like this:

$image = $row['image']; $image_type= $row['image_type']; $size = $row['image_size']; $ext = explode('/', $image_type); $name = $id . '.' . $ext[1];

header("Content-type: $image_type"); header("Content-length: $size"); header("Content-Disposition: attachment; filename=$name");

print $image;
exit;

Upvotes: 0

Grish
Grish

Reputation: 862

Don't store the image in the database. store it in your file system. store name and location of the file in the database. Because

1.If database is corrupted, no way to retrieve.

2.Retrieving image files from db is slow when compared to other option.

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157828

That's simple.
Just don't store an image in database in a blob field. Instead, store the image itself on a filesystem, while in database store only name of the file. this way your code would work.

echo '<h4>'.$yaz["Header"].'</h4><br><img src='.$yaz["Picture"].'><p class="text">'.$yaz["Description"].'</p><br>';

Upvotes: 3

Related Questions