加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

在PHP中显示来自PostgreSQL数据库的图像

发布时间:2020-12-13 21:50:57 所属栏目:PHP教程 来源:网络整理
导读:我想在 PHP文件中显示PostgreSQL数据库中的图像. 在尝试了很多不同的可能性后,我不会走得太远. 这是我做的: 上传: echo "div". "table border="1" cellpadding="3"". "form action="test2.php" method="post" enctype="multipart/form-data"". "
我想在 PHP文件中显示PostgreSQL数据库中的图像.
在尝试了很多不同的可能性后,我不会走得太远.
这是我做的:

上传:

echo "<div>".
    "<table border="1" cellpadding="3">".
       "<form action="test2.php" method="post" enctype="multipart/form-data">".
         "<tr>".
           "<td>".
             "<input type="file" name="file" id="file">".
             "<input class="button" type="submit" name="submit" value="Submit">".
           "<tr>".
         "</tr>".
      "</form>".
     "</table>".
   "</div>";

显示:

if(isset($_FILES['file'])) //file uploaded
{
  $file_raw = file_get_contents($_FILES['file']['tmp_name']);
  $file     = pg_escape_bytea($file_raw);
  //Here is Code to insert into Database but just to test,I try it directly:

  header('Content-Type: image/png');
  echo pg_unescape_bytea($file);
}

我已将显示部分放在一个额外的文件中,依此类推,但这些是您需要了解的基本信息.

我不会显示任何图像,只是浏览器中的“Broken Image”图标.
这有什么不对?我怎么解决这个问题?
谢谢!

解决方法

处理简单:

<?php 
// Connect to the database
$dbconn = pg_connect( 'dbname=foo' );

// Read in a binary file
$data = file_get_contents( 'image1.jpg' );

// Escape the binary data to avoid problems with encoding
$escaped = bin2hex( $data );

// Insert it into the database
pg_query( "INSERT INTO gallery (name,data) VALUES ('Pine trees',decode('{$escaped}','hex'))" );

// Get the bytea data
$res = pg_query("SELECT encode(data,'base64') AS data FROM gallery WHERE name='Pine trees'");  
$raw = pg_fetch_result($res,'data');

// Convert to binary and send to the browser
header('Content-type: image/jpeg');
echo base64_decode($raw);
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读