开发者

Load file from mysql with PHP

I store images in my mysql database as blob. I can load these files with a query in a variable, but how can I send back the contents of this variable to the b开发者_如何学运维rowser as an image?

Can the html file contain something like this <img src="smtg.png"> ? Or how the request can be made?


You need a script that echos out the image data. Use header() to set the appropriate content type, and then echo the data. For example:

header("Content-type: image/png");
...
echo $db_results['imgdata'];

Then, call your script from the HTML page like this:

<img src="yourimagescript.php" />

Ideally, you should be storing file type in a column next to your image data, so you can dynamically set the correct content type.


PHP

<?php
mysql_connect("localhost","test","test");
mysql_select_db("test");
$imageid = (int)$_GET['imgid'];
$rs = mysql_query("select * from images where imageid=$imageid");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row['imgdata'];
header("Content-type: image/jpeg");
print $imagebytes;
?>

HTML

<img src="location_to_your_php_script.php?imgid=21" alt="xxx" />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜