如何使用PHP显示存储在mysql数据库中的图像 [英] how to display images stored in a mysql database using PHP

查看:101
本文介绍了如何使用PHP显示存储在mysql数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图使用php显示数据库中的图像,但是每当我使用或单击链接时,我只会得到下载提示.我想做的是,我想在网络浏览器上显示它.并想在标签中使用它.

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.

我的代码是:

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.

因此,当我从mysql数据库中检索图像时,我应该怎么做才能在网页上显示图像

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

推荐答案

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]);
}

这篇关于如何使用PHP显示存储在mysql数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆