duanjinchi1982 2010-08-16 08:10
浏览 139
已采纳

如何使用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 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]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型