duanjinchi1982
2010-08-16 08:10 阅读 78
已采纳

如何使用PHP显示存储在mysql数据库中的图像

I have been trying to display image from database using php but i am only getting download prompt whenever i use or click the link. what i want to do is, i want to display it on the web browser. And want to use it in tags.

my code is:

require_once('dbconfig.php');
        $cont=mysql_connect($dbhost,$dbuser,$dbpass) or die("Error Connecting the Database");
        $seldatabase=mysql_select_db($dbselect,$cont);
        $insert = "SELECT `p_mime`, `p_name`, `p_size`, `p_data` FROM $dbtbl_reg_details WHERE `id` = $id";
        $query = mysql_query($insert);
        if ($query)
        {
            if (mysql_num_rows($query)==1)
            {
                $row = mysql_fetch_assoc($query);
                header("Content-Type: image/png");
                header("Content-Length: ". $row['p_size']);
                header("Content-Disposition: inline; filename=". $row['p_name']);
                echo $row[p_data];
            }
        else
        {
            echo "image with id = ".$id." does not exist";
        }
    }
    else
    {
        echo "query failed, image with id = ".$id." does not exist";
    }

And when i use inline for Content-Disposition: then my web page returns a script error on the browser.

So, what should i do to display images on web pages while retrieving it from mysql database

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongxing2302 dongxing2302 2010-08-16 09:41
    if (mysql_num_rows($query)==1)
    {
        while( @ob_end_clean() );
    
        header("Content-type:  image/png");
        header("Content-Length: ". $row['p_size']);
        header("Content-Disposition: attachment; filename=\"{$row['p_name']}\"");
    
        die($row[p_data]);
    }
    
    点赞 评论 复制链接分享
  • dsakktdog498483070 dsakktdog498483070 2010-08-16 08:12

    why u have echo hi; ?

    its probably cause an error

    and also add exit in the end (to sure you dont echo anything after)

    echo $row[p_data];
    exit;
    
    点赞 评论 复制链接分享

相关推荐