简体   繁体   中英

Cannot display Image from database in php

I am using smarty, mysql and I am just trying to display image. This is the error i am getting -

Resource interpreted as Image but transferred with MIME type text/html

My index.php file

<img src="http://localhost/admin/image2.php?id={$editWine[0].id}" width="150" height="260" border="0" class="bottle-img" id="smallImageWineEdit" />

My image2.php file

$id=$_REQUEST['id'];        
$sql="SELECT * FROM table WHERE id=$id";        
$result=mysql_query($sql) or die(mysql_error());
while($row=@mysql_fetch_assoc($result))
{
   echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image_data'] ).'" width="150" height="150" /> &nbsp';  
   echo $row['image_data'];                     
}

This echo inside while loop is working fine.

And when i inspect element and open this img link in new tab, Image is displaying. whereas its not displaying in current page.

Issue is that you are again delivering the html tags from the image file. You just need to output the image data with proper image content-type.

Edit image2.php like

if($row=@mysql_fetch_assoc($result))
{
   header('Content-type: image/jpg');
   echo $row['image_data'];
   exit();                   
}

update to comments below

Do the above changes and request http://localhost/admin/image2.php?id=VALID_ID_HERE in browser and check if its retuning an image. Then you can use it in the src tag of index.php to show it there. If you get errors in rendering the image, whn you request it, make sure you are asking for the correct image id in the db and update the question with the error messages you are seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM