开发者

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

开发者 https://www.devze.com 2023-01-23 03:08 出处:网络
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 in开发者_如何学Gostead:

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 in开发者_如何学Gostead:

$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;


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

header("Content-Disposition: inline; filename=$name");


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; ?>"


you need to be sure nothing else than your headers and the image content is sent to the client. maybe you want to do an exit after echo $content; this is not a best practice and also does not ensure that nothing else has been sent before you output the image content, but it should do the job.


You can display the image instead of downloading it with this code:

<a href="myimage.png">Click to download</a>
0

精彩评论

暂无评论...
验证码 换一张
取 消