【问题标题】:Display image from PostgreSQL database in PHP在 PHP 中显示来自 PostgreSQL 数据库的图像
【发布时间】:2014-04-08 06:28:49
【问题描述】:

我想在 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);
}

我已经在一个额外的文件等中有显示部分,但这些是您需要了解的基本信息。

我不会显示任何图像,只有浏览器中的这个“损坏的图像”图标。 这里有什么问题?我该如何解决这个问题? 谢谢!

【问题讨论】:

    标签: php postgresql png bytearray


    【解决方案1】:

    简单处理:

    <?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);
    ?>
    

    【讨论】:

      【解决方案2】:

      简短的回答是pg_unescape_bytea 不一定是pg_escape_bytea 的倒数。这在关于 PQunescapeBytea 的 libpq 文档中有所提及:

      这种转换并不完全是 PQescapeBytea 的逆转换,因为 从 PQgetvalue

      回到 php,您需要在数据库中进行一次往返才能使测试有意义,例如:

      $pgconn = pg_connect("....");
      $escaped = pg_escape_bytea($pgconn, $bytes);
      $pgr = pg_query($pgconn, "SELECT '$escaped'::bytea");
      list($textual) = pg_fetch_array($pgr);
      $raw_bytes = pg_unescape_bytea($textual);
      

      在这种情况下,$raw_bytes 在所有情况下都与初始的$bytes 相同。


      顺便说一句,如果您想深入挖掘,还有更多内容。

      pg_escape_bytea 将连接到数据库的资源作为可选参数:

      string pg_escape_bytea ([ resource $connection ], string $data )
      

      无论是否使用连接,其行为都会有所不同(当 $connection 被忽略但之前已调用 pg_connect() 时,无论如何都会使用“当前连接”)。

      使用连接时,其输出由连接属性驱动,具体而言:

      • PG 服务器是否足够新 (>=9.0) 以支持字节字符串中的十六进制序列 \x...
      • standard_conforming_stringsonoff(>=9.1 时默认为on

      不同配置下相同输入的不同结果示例:

      服务器 9.1、客户端 9.1、php 5.3.10

      代码 #1:

      // No connection
      // pg_connect("dbname=test");
      echo pg_escape_bytea(chr(0).chr(1));
      

      结果 #1:

      \\000\\001

      代码 #2:

      // With connection
      $pgconn= pg_connect("dbname=test");
      echo pg_escape_bytea(chr(0).chr(1));
      

      结果 #2:

      \x0001

      代码 #3:

      // With connection
      $pgconn= pg_connect("dbname=test");
      pg_query("SET standard_conforming_strings TO off");
      echo pg_escape_bytea(chr(0).chr(1));
      

      结果 #3:

      \\x0001

      现在使用旧配置:8.4 客户端,8.4 服务器,php 5.2.6

      代码 #4:

      $pgconn= pg_connect("dbname=test");
      pg_query("SET standard_conforming_strings TO off");
      echo pg_escape_bytea(chr(0).chr(1));
      

      结果 #4:

      \\000\\001

      代码 #5:

      $pgconn= pg_connect("dbname=test");
      pg_query("SET standard_conforming_strings TO on");
      echo pg_escape_bytea(chr(0).chr(1));
      

      结果 #5:

      \000\001

      现在我们在正常使用中不需要关心所有这些。由于服务器将正确解释结果,因此只需按预期使用它的连接即可。关键是不要假设pg_escape_bytea 是一个独立于服务器的固定可逆转换,如bin2hex 或类似的,因为它不是。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-02
        • 2015-01-24
        • 2020-05-19
        • 2012-02-03
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        • 1970-01-01
        相关资源
        最近更新 更多