Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to display an image instead of downloading it (Image in database blob)

Tags:

php

I want display an image instead of downloading it.

I have image in my database table, column as BLOB.

This snippet downloads the image, but I want to display it instead:

$query = "SELECT * FROM upload";
$result  = mysql_query($query);
$row = mysql_fetch_array($result);
$content =  $row['content'];
$size =  $row['size'];
$type =  $row['type'];
header("Content-length: $size");
header("Content-type: $type");

// The following headers make the image download, but I don't want it to
// download, I want to show the image. What should I do?
// header("Content-Disposition: attachment; filename=$name");

echo $content;
like image 452
Bharanikumar Avatar asked Nov 03 '10 16:11

Bharanikumar


2 Answers

The opposite content-disposition of attachment is inline. Try this:

header("Content-Disposition: inline; filename=$name");
like image 75
meagar Avatar answered Oct 21 '22 06:10

meagar


if you want to use this more dynamically make a script of your original code and call it like this:

<img src="image.php?imageid=$myImageID" />

and your script is:

$myImageID = $_GET["myImageID"];
$query = "SELECT * FROM upload where id='"+$myImageID+"'";
$result  = mysql_query($query);
$row = mysql_fetch_array($result);
$content =  $row['content'];
$size =  $row['size'];
$type =  $row['type'];
header("Content-length: $size");
header("Content-type: $type");
//header("Content-Disposition: attachment; filename=$name");---> this headers make system to download , but i dont want to download, i want to show image, what  should i do ,
echo $content; ?>"
like image 45
CristiC Avatar answered Oct 21 '22 05:10

CristiC