使用yiiframework将图像存储到postgresql bytea字段中

时间:2014-03-26 12:46:27

标签: php postgresql yii

我正在尝试将图像存储到postgresql bytea字段中。

$data=file_get_contents("filename.jpg");
pg_escape_bytea($data);

然后我将其插入数据库。之后,当我从数据库中选择图像时,它显示的是Resource_id#。如果我在所选字段上使用pg_unescape_bytea(),则它显示空值。

如何使用yii存储和检索postgresql中的数据?

2 个答案:

答案 0 :(得分:1)

  

Yii DAO is built on top of PHP Data Objects (PDO)

使用PDO,您不应使用pg_*函数,因为它们属于不同的扩展名。

如果您确实要将图像存储在bytea中,则应使用prepare / bind机制(或其Yii-DAO equivalent)来存储图像内容,应该知道在(postgres)PDO中,bytea类型作为stream返回,因此您可以使用fread()stream_get_contents()等函数来阅读它。

答案 1 :(得分:0)

我的方式:

use yii\db\Expression;

$upload = UploadedFile::getInstance($model, 'upload');
$data = pg_escape_bytea(file_get_contents($upload->tempName));
$model->myfield = new Expression("'{$data}'");

要检索:

$data = stream_get_contents($model->myfield);
相关问题