php - Image from MySQL database not printing -
the query , html table printing out $row["title"], date, , row["text1"] great. however, there problem $row["file_data"], supposed image stored in mysql database. it's printing out large number of characters languages other english, i. e. bmö26(šÀ2ÿÿÿÿÿÿÿÿ, etc. several dozen rows.
how can make image appear?
thanks in advance,
john
$sqlstr = "select s.title, s.datesubmitted, s.text1, s.text2, s.text3, s.cleanurl1, s.cleanurl2, s.cleanurl3, s.submissionid, i.image_id, i.filename, i.mime_type, i.file_size, i.file_data, i.photonumber submission s join images2 on s.submissionid = i.submissionid group s.submissionid order s.datesubmitted desc limit $offset, $rowsperpage"; $tzfrom = new datetimezone('america/new_york'); $tzto = new datetimezone('america/phoenix'); // echo $dt->format(date_rfc822); $result = mysql_query($sqlstr); //header('content-type: image/bmp'); $arr = array(); echo "<table class=\"samplesrec\">"; while ($row = mysql_fetch_array($result)) { //header('content-type: ' . $row['mime_type']); //header('content-length: ' . $row['file_size']); $dt = new datetime($row["datesubmitted"], $tzfrom); $dt->settimezone($tzto); echo '<tr class="class3a">'; echo '<td class="sitename1"><a href="http://www...com/blog">'.$row["title"].'</a></td>'; echo '</tr>'; echo '<tr class="class3b">'; echo '<td class="sitename2name">'.$dt->format('f j, y &\nb\sp &\nb\sp g:i a').'</td>'; echo '</tr>'; echo '<tr class="class3c">'; echo '<td class="sitename2">'.$row["text1"].'</td>'; echo '</tr>'; echo '</tr>'; echo '<tr class="class3c">'; echo '<td class="sitename2">'.$row["file_data"].'</td>'; echo '</tr>'; } echo "</table>";
it's because browser doesn't realize data you're sending image. when web server responds request specified type of content (hence content-type header), , page being specified text. why image tags used: give chance "embed other resource in location". code dump binary data of image text onto screen - not want.
what need create php page, such getimage.php, accepts $_get parameter (ie., row id). page query database , echo image data, specifying content-type header.
here proof of concept code wrote without testing , not handle sql injection, or number of other potential issues.
header('content-type: image/png'); //change proper content type type of image $imageid = mysql_real_escape_string($_get['q']); $result = mysql_query(sprintf('select file_data images2 id="%s" , file_data not null limit 1', $_get['q'])); if(mysql_num_rows($result) !== 1) { //a row wasn't found, 404 header('http/1.0 404 file not found'); } else { $row = mysql_fetch_object($result); echo $row['file_data']; } now, when you're building html in existing file, this:
echo '<td class="sitename2"><img src="./getimage.php?q='.$row["id"].'"/></td>'; adjusting sql column names accordingly.
cheers.
Comments
Post a Comment