gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:21:11 26 4
gpt4 key购买 nike

我想在 PHP 文件中显示来 self 的 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 
// 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);
?>

关于php - 在 PHP 中显示来自 PostgreSQL 数据库的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22210612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com