我一直将 zip 文件的内容存储在 MySQL 数据库的 LONGBLOB 中,但我正在转向 Postgres。据我所读,在 Postgres 中与 LONGBLOB 等价的是 bytea。不幸的是,我无法将 zip 内容写入数据库。首先,我得到了这个错误:
Warning: pg_query(): Query failed: ERROR: invalid byte sequence for encoding "UTF8"
0x5c
然后我用 SQL_ASCII 编码创建了一个新数据库(之前有 UTF8),并得到了这个错误:
PHP Warning: pg_query(): Query failed: ERROR: syntax error at or near "v╘▓ú5"
LINE 2: ...√╢V[úB√▬┌»å⌐²└ù╙±b±≡ß▼┐π}°ï»┌G╜₧╧εo»~°0o╞W/_╝ƒL¢\'v╘▓ú5;Ω░#╩...
有没有办法在不编码的情况下在 Postgres 中创建数据库/表/列?如果没有,我应该如何存储这些信息?我尝试使用 pg_escape_bytea 但是当我尝试解压缩内容时会产生错误。不确定这是否真的重要,但这是我用来写入数据库的 PHP:
$content = file_get_contents($zipLocation);
$content = addslashes($content);
$sql="INSERT INTO ZIP_TBL ( BUILD, CONTENT)
VALUES ('$build', '$content')";
if (!pg_query($con,$sql)) {
die('Error: ' . pg_last_error($con));
}
最佳答案
bytea
输入格式之一是十六进制。所以使用bin2hex
$content = file_get_contents($zipLocation);
$content = '\\x' . bin2hex($content);
$sql="
insert into zip_tbl ( build, content)
values ('$build', E'$content')
";
http://www.postgresql.org/docs/current/static/datatype-binary.html#AEN5318
http://php.net/manual/en/function.bin2hex.php
顺便说一句,如果您不知道,您很容易受到 SQL 注入(inject)攻击。
关于php - 在 Postgres 中存储 Zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25126891/