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

时间:2013-05-09 14:30:03

标签: php mysql

我正在尝试显示图片,但我的网页却显示了编码内容。以下是我的代码:

<?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();

?>

我正在使用$ _GET关联数组($ _ GET [&#39;变量&#39;])通过另一页上的链接获取产品ID。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

我不知道Content-type标题是否挑剔,但更改了冒号周围的间距(image/jpg也应该是image/jpeg):

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

根据下面的答案,我同意 - 此修补程序假设此脚本仅用于在HTML中显示图片,ala <img src="path/to/your/image.php?productid=123" />

image/jpeg MIME type spec here上的进一步阅读。