【问题标题】:Store pdf remote file in postgres DB with php使用 php 将 pdf 远程文件存储在 postgres DB 中
【发布时间】:2011-03-11 11:49:51
【问题描述】:

我有很少的 pdf(或 txt、odt、doc)文件要使用 php 存储在 Postgres DB 中。 我使用这个 php 函数来读取文件:

$fp = fopen($name, 'rb');

$content = fread($fp, 文件大小($name));

$content = addlashes($content);

fclose($fp);

然后尝试存储在db中:

$sql = "插入表(列)值('$content'::bytea)";

$result = @pg_query($sql);

"column" 是 bytea 类型。

当我使用 pdf 文件执行脚本时,出现以下错误:

错误:用于编码“UTF8”的无效字节序列:0xe2e3cf 提示:如果字节序列与服务器预期的编码不匹配(由“client_encoding”控制),也会发生此错误。

当我使用 doc 文件执行脚本时,出现以下错误:

错误:bytea 类型的输入语法无效

当我使用 txt 文件执行脚本时,没有错误:

出了什么问题,存储文件的正确方法是什么?

【问题讨论】:

    标签: php database postgresql pdf file-upload


    【解决方案1】:

    您是否尝试过使用pg_escape_string 而不是addslasheshttp://www.php.net/manual/en/function.pg-escape-string.php

    【讨论】:

    • 谢谢,pg_escape_bytea() 而不是addslashes() 正是我想要的。
    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多