Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

php - displaying an image stored in a mysql blob

when i run the code below it displays an image that is stored in a mysql Db as a blob variable. The problem is if I echo out anything else, even something as simple as echo '--------'; before I call the image, the image will not display. only random characters display. if i echo out anything after the image the image displays but nothing does after it. Can someone tell me why this is and how i can go about displaying other items and the image together, thanks

<?php

include("inc/library.php");

connectToDatabase();

$sql = "SELECT * FROM theBlogs WHERE ID = 1;";

$result = mysql_query($sql) or die(mysql_error());  
$row = mysql_fetch_array($result);

header("Content-type: image/jpeg");
echo $row['imageContent'];
$db->close();

?>
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can convert the image data into base64 and stick it in an <img> tag. Currently, you are trying to write text outside of the image data, and the internet browser thinks your text is part of the image and thus throws an error.

Try something like this:

echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';
echo 'Hello world.';

Note, this isn't the best solution because it cannot be cached and is fairly slow, especially on mobile phones. Check out the caniuse for data URIs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...