mysql blob 如何显示_如何从mysql blob显示图像

bd96500e110b49cbb3cd949968f18be7.png

I am trying to display an image from a MySQL blob field. I have tried a few different things and none of them seem to work.

I have tried:

header("Content-type: $type"); img src = $blobData;

header("Content-type: $type"); echo($blobData);

解决方案

Another option you might consider (assuming you are on Apache):

Create an .htaccess file with a mod_rewrite for all image extensions (png, jpg, gif).

Have it redirect to a php script that looks up the image requested in the DB. If it is there, it echos out the header and BLOG. If it isn't there, it returns a standard 404.

This way you can have:

adorablepuppy.jpg

Which then gets redirected ala:

RewriteEngine on

RewriteRule \.(gif|jpg|png)$ imagelookup.php

This script does a query for the image, which (obviously) assumes that the requested image has a unique key that matches the filename in the URL:

$url = $_SERVER['REQUEST_URI'];

$url_parts = explode("/", $url);

$image_name = array_pop($url_parts);

Now you have just the image filename. Do the query (which I shall leave up to you, along with any validation methods and checks for real files at the address, etc.).

If it comes up with results:

header('Content-type: image/jpeg');

header('Content-Disposition: inline; filename="adorablepuppy.jpg"');

print($image_blog);

otherwise:

header("HTTP/1.0 404 Not Found");

FYI: I have no idea if this would be bad in terms of performance. But it would allow you to do what I think you want, which is output the image as though it were a flat image file on the server using a simple image element. I'm inclined to agree that BLOBs are not the best way to go, but this does avoid any cross-browser issues.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值