使用php将pdf远程文件存储在postgres数据库中
我有很少的 pdf(或 txt、odt、doc)文件要使用 php 存储在 Postgres DB 中。 我使用这个php函数来读取文件:
$fp = fopen($name, 'rb'); $content = fread($fp, filesize($name)); $content = addslashes($content); fclose($fp);
然后尝试存储在数据库中:
$sql = "insert into Table(column) values ('$content'::bytea)"; $result = @pg_query($sql);
“column”是bytea类型。
当我使用 pdf 文件执行脚本时,出现以下错误:
ERROR: invalid byte sequence for encoding "UTF8": 0xe2e3cf HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
当我使用 doc 文件执行脚本时,出现以下错误:
ERROR: invalid input syntax for type bytea
当我使用 txt 文件执行脚本时,没有错误:
出了什么问题,正确的方法是什么存储文件?
I have little pdf (or txt, odt, doc) file to be store in Postgres DB with php.
I use this php functions to read file:
$fp = fopen($name, 'rb'); $content = fread($fp, filesize($name)); $content = addslashes($content); fclose($fp);
and then try to store in db:
$sql = "insert into Table(column) values ('$content'::bytea)"; $result = @pg_query($sql);
"column" is bytea type.
When I execute the script with pdf file, I get the follow error:
ERROR: invalid byte sequence for encoding "UTF8": 0xe2e3cf HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
When I execute the script with doc file, I get the follow error:
ERROR: invalid input syntax for type bytea
When I execute the script with txt file, NO error:
What's wrong and what is the proper way to store files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用
pg_escape_string
而不是addslashes
? http://www.php.net/manual/en/function .pg-escape-string.phpHave you tried using
pg_escape_string
instead ofaddslashes
? http://www.php.net/manual/en/function.pg-escape-string.php