简体   繁体   English

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

[英]Display image from PostgreSQL database in PHP

I want to display an image from my PostgreSQL database in a PHP file. 我想在PHP文件中显示PostgreSQL数据库中的图像。 After trying a lot of different possibilities, I don't get very far. 在尝试了很多不同的可能性后,我不会走得太远。 Here is what I do: 这是我做的:

Upload: 上传:

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

Display: 显示:

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

I already have the display part in an extra file and so on, but these are the essential information you need to know. 我已将显示部分放在一个额外的文件中,依此类推,但这些是您需要了解的基本信息。

I wont get any image displayed, just this "Broken Image" icon from the browser. 我不会显示任何图像,只是浏览器中的“Broken Image”图标。 What is wrong here? 这有什么不对? How can I solve this? 我怎么解决这个问题? Thank you! 谢谢!

Simple handling: 处理简单:

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

The short answer is that pg_unescape_bytea is not necessarily the inverse of pg_escape_bytea . 简短的回答是pg_escape_bytea不一定是pg_unescape_bytea的反转。 This is mentioned in libpq documentation about PQunescapeBytea : 有关PQunescapeBytea的 libpq文档中提到了这一点:

This conversion is not exactly the inverse of PQescapeBytea, because the string is not expected to be "escaped" when received from PQgetvalue 此转换与PQescapeBytea不完全相反,因为从PQgetvalue收到该字符串时不会被“转义”

Back to php, you'd need to do a round-trip through the database for the test to be meaningful, for example: 回到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);

In this case $raw_bytes will be identical to the initial $bytes , in all cases. 在这种情况下, $raw_bytes将与初始$bytes相同。


On a side note, if you want to dig deeper, there's much more to it. 另外,如果你想深入挖掘,还有更多内容。

pg_escape_bytea takes a connection resource to a database as an optional argument: pg_escape_bytea将连接资源作为可选参数传递给数据库:

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

Its behavior differs whether a connection is used or not (when $connection is left out but pg_connect() has been called before, the "current connection" will be used anyway). 无论是否使用连接,它的行为都不同(当省略$connection但之前调用了pg_connect() ,无论如何都将使用“当前连接”)。

When a connection is used, its output is driven by the connection properties, specifically: 使用连接时,其输出由连接属性驱动,具体为:

  • is the PG server recent enough (>=9.0) to support hex sequences \\x... in bytea strings? PG服务器是否足够支持(> = 9.0)以支持bytea字符串中的十六进制序列\\x...
  • is standard_conforming_strings to on or off ( on by default when >=9.1) 是将standard_conforming_strings onoff (默认情况下在> = 9.1时on

Examples of different results with the same input in different configurations: 在不同配置中具有相同输入的不同结果的示例:

Server 9.1, client 9.1, php 5.3.10 Server 9.1,客户端9.1,php 5.3.10

Code #1: 代码#1:

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

Result #1: 结果#1:

\\000\\001

Code #2: 代码#2:

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

Result #2: 结果#2:

\x0001

Code #3: 代码#3:

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

Result #3: 结果#3:

\\x0001

Now with an older configuration: 8.4 client, 8.4 server, php 5.2.6 : 现在配置较旧: 8.4客户端,8.4服务器,php 5.2.6

Code #4: 代码#4:

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

Result #4: 结果#4:

\\000\\001

Code #5: 代码#5:

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

Result #5: 结果#5:

\000\001

Now we don't need to care about all this in normal usage. 现在我们不需要在正常使用中关心所有这些。 Just using it as intended with its connection will work since the server will interpret the results correctly. 由于服务器将正确解释结果,因此只需按预期使用它即可。 The point is to not assume that pg_escape_bytea is a server-independant fixed reversible transformation like bin2hex or some such, because it's not. 关键是不要假设pg_escape_bytea是一个服务器独立的固定可逆转换,如bin2hex或其他一些,因为它不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM