簡體   English   中英

在PHP中顯示來自PostgreSQL數據庫的圖像

[英]Display image from PostgreSQL database in PHP

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

簡短的回答是pg_escape_bytea不一定是pg_unescape_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)以支持bytea字符串中的十六進制序列\\x...
  • 是將standard_conforming_strings onoff (默認情況下在> = 9.1時on

在不同配置中具有相同輸入的不同結果的示例:

Server 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或其他一些,因為它不是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM