代码之家  ›  专栏  ›  技术社区  ›  Shiv Deepak

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

  •  0
  • Shiv Deepak  · 技术社区  · 15 年前

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

    当我使用内联处理内容时:我的网页在浏览器上返回一个脚本错误。

    2 回复  |  直到 15 年前
        1
  •  1
  •   ovais.tariq    15 年前
    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]);
    }
    
        2
  •  0
  •   Haim Evgi    15 年前

    echo hi;

    它可能会导致一个错误

    还有 在末尾添加exit(以确保后面没有回声)

    echo $row[p_data];
    exit;