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

发布于 2024-09-14 19:53:07 字数 1104 浏览 1 评论 0原文

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

我的代码是:

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";
    }

当我使用内联进行内容处置时:然后我的网页在浏览器上返回脚本错误。

那么,我应该怎么做才能在网页上显示图像,同时从 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-09-21 19:53:07
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]);
}
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]);
}
小傻瓜 2024-09-21 19:53:07

为什么你有echo hi;

它可能会导致错误

,而且
最后添加 exit (以确保您不会回显之后的任何内容)

echo $row[p_data];
exit;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文