简体   繁体   English

使用php从mysql数据库中读取图像文件

[英]Reading an Image file from a mysql database using php

I am trying to display an image, but my webpage displays encoding stuff instead. 我正在尝试显示图像,但是我的网页上却显示编码内容。 Below is my code: 下面是我的代码:

<?php ob_start();?>

// html markups goes here

<?php include 'login.php';
if(isset($_GET['productid'])){
    $productid = $_GET['productid'];

    $sql = "select tyre_image from tyres where product_id = '$productid'";
    $result = mysql_query($sql) or die(mysql_error());
    header("Content-type :image/jpg");
    echo mysql_result($result,0);

}

ob_end_flush();

?>

I am using $_GET associative array($_GET['variable']) to get the product ID via a link on another page. 我正在使用$ _GET关联数组($ _GET ['variable'])通过另一页上的链接获取产品ID。

How would I fix this? 我该如何解决?

I had no idea the Content-type header was this picky, but change the spacing around the colon (and also image/jpg should be image/jpeg ): 我不知道Content-type标头是不是很挑剔,但是更改了冒号周围的间距(而且image/jpg应该是image/jpeg ):

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

Per the answer below, I agree - this fix assumes that this script is just used for displaying an image in your HTML, ala <img src="path/to/your/image.php?productid=123" /> . 根据以下答案,我同意-此修复程序假定此脚本仅用于显示HTML中的图像,例如<img src="path/to/your/image.php?productid=123" />

Further light reading on the image/jpeg MIME type spec here . 此处进一步阅读image/jpeg MIME类型规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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