【问题标题】:how to display images stored in a mysql database using PHP如何使用 PHP 显示存储在 mysql 数据库中的图像
【发布时间】:2010-08-16 19:11:06
【问题描述】:

我一直在尝试使用 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";
    }

当我对 Content-Disposition 使用内联时:然后我的网页在浏览器上返回一个脚本错误。

那么,在从 mysql 数据库中检索图像时,我应该如何在网页上显示图像

【问题讨论】:

标签: php mysql blob


【解决方案1】:
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]);
}

【讨论】:

  • 嗨!这个 "' . $row['p_name'] . '"' 给我一个语法错误
【解决方案2】:

为什么你有echo hi;?

它可能会导致错误

还有 最后添加退出(确保你不会回显任何内容)

echo $row[p_data];
exit;

【讨论】:

  • 嗨,我已经删除了 echo hi 并将 Content-Disposition: 设置为内联。现在出现错误“无法显示图像“127.0.0.1/isc2011/photo.php?id=2”,因为它包含错误。”我已经正确上传了文件,可能是什么原因!?
猜你喜欢
  • 2015-10-26
  • 2012-10-24
  • 2015-01-01
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
相关资源
最近更新 更多