2011-03-11 109 views
1

我使用php在Postgres DB中存儲了一點pdf(或txt,odt,doc)文件。 我用這個PHP函數讀取文件:在php中使用postgres DB存儲pdf遠程文件

$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);

「列」 是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文件執行腳本,否錯誤:

什麼是錯,什麼是正確的方式來存儲文件?

回答