Laravel - using a postgre bytea blob field

青春壹個敷衍的年華 提交于 2019-12-01 21:16:42

问题


I am using PostgreSQL on a Laravel installation. A table has a bytea type field which is being used to store binary data (base64_encoded file contents).

When I use Eloquent to retrieve the table I get a resource type variable being returned in this field.

How can I rather retrieve this as a string?

$raw = Media::where('id','=',$id)->first();
$raw->file_data = base64_decode($raw->file_data);   // doesn't work

回答1:


As the author of this question did not post the details to the answer, I will post my findings here.

As the returned field is a handle to a stream you can use the stream_get_contents function to read the value into a string, you can then use pg_unescape_bytea to get the actual value of the bytea data. Finally use the htmlspecialchars function if you wish to display the bytea data in HTML.

Example code:

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



回答2:


The answer was to use stream_get_contents on the stream. duh.



来源:https://stackoverflow.com/questions/11329323/laravel-using-a-postgre-bytea-blob-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!