使用php将pdf远程文件存储在postgres DB中
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?
我有一些pdf(或txt,odt,doc)文件存放在带有php的Postgres数据库中。 I 使用这个php函数来读取文件: p>
$ fp = fopen($ name,'rb'); $ content = fread($ fp,filesize($ name)) ; $ content = addslashes($ content); nclclose($ fp); pre>然后尝试存储在db中: p>
$ sql =“insert into Table(column)values('$ content':: bytea)”; $ result = @pg_query($ sql); pre>“column”是bytea类型。 p>
当我用pdf文件执行脚本时,出现以下错误: p>
ERROR:无效的字节序列 编码“UTF8”:0xe2e3cf提示:如果字节序列与服务器期望的编码不匹配,也会发生此错误,该编码由“client_encoding”控制。 pre>当我执行 带有doc文件的脚本,我收到以下错误: p>
错误:类型为bytea的输入语法无效 pre>当我用txt文件执行脚本时, 没有错误: p>
出了什么问题 存储文件的正确方法是什么? p> div>
Have you tried using pg_escape_string
instead of addslashes
? http://www.php.net/manual/en/function.pg-escape-string.php