Laravel - 使用 postgre bytea blob 字段

问题描述 投票:0回答:2

我在 Laravel 安装上使用 PostgreSQL。表有一个字节类型字段,用于存储二进制数据(base64_encoded 文件内容)。

当我使用 Eloquent 检索表时,我会在此字段中返回一个资源类型变量。

我怎样才能将其作为字符串检索?

$raw = Media::where('id','=',$id)->first();
$raw->file_data = base64_decode($raw->file_data);   // doesn't work
php postgresql-9.1 laravel
2个回答
10
投票

由于这个问题的作者没有发布答案的详细信息,我将在这里发布我的发现。

由于返回的字段是流的句柄,因此您可以使用 stream_get_contents 函数将值读取到字符串中,然后可以使用 pg_unescape_bytea 获取 bytea 数据的实际值。如果你想在 HTML 中显示 bytea 数据,最后使用 htmlspecialchars 函数。

示例代码:

$my_bytea = stream_get_contents($resource);
$my_string = pg_unescape_bytea($my_bytea);
$html_data = htmlspecialchars($my_string);

1
投票

答案是在流上使用stream_get_contents。呃。

© www.soinside.com 2019 - 2024. All rights reserved.