Home > php教程 > php手册 > body text

使用PostgreSQL的bytea字段存读取文件及读取出错问题处理

WBOY
Release: 2016-06-13 11:29:43
Original
1827 people have browsed it

  PostgreSQL中的bytea字段类型可以以二进制的形式存储数据,这样做的好处就是可以将原本存储在网站目录下的文件存储到数据库中,坏处就是如果文件过多、过大的话,就会导致数据库的数据量大大增加,备份和恢复的时候就会浪费大量的时间,而且数据也有可能会出错。个人觉得,在文件量小的情况下,使用这种存储方式还是很方便的。

  言归正传,下面介绍一下使用bytea字段存读取文件的具体实现方法。首先是文件存储于存储于bytea字段的方法,主要用到的就是PHP中的pg_escape_bytea方法,代码如下:

<?<span php
</span><span $dbconn</span> = pg_connect("host='localhost' dbname='dbname' user='user' password='password' port='port'"<span )
    OR </span><span DIE</span>('Could not connect:' .<span  pg_last_error());
</span><span $fileUrl</span> = <span iconv</span>("utf-8", "gbk", <span $fileUrl</span>);<span //</span><span $fileName为文件路径地址,汉字可能会乱码,此处处理一下</span>
<span $fileContents</span> = <span file_get_contents</span>(<span $fileUrl</span><span );
</span><span $escapeBytea</span> = pg_escape_bytea(<span $fileContents</span>);<span //</span><span 转义bytea数据类型的二进制字符串</span>
<span $insertStr</span> = "<span INSERT INTO tableName(id, contents)
            VALUES(DEFAULT, '{</span><span $escapeBytea</span>}')"<span ;
pg_query(</span><span $dbconn</span>, <span $insertStr</span><span );
</span>?>
Copy after login

  从bytea字段中还原文件用到的是PHP中的pg_unescape_bytea方法,实现代码如下:

<?<span php
    </span><span $dbconn</span> = pg_connect("host='localhost' dbname='dbname' user='user' password='password' port='port'"<span )
                    OR </span><span DIE</span>('Could not connect: ' .<span  pg_last_error());
    </span><span $selectStr</span> = "<span SELECT contents
                FROM tableName
                WHERE id = </span>" . <span $fileId</span>;<span //</span><span $fileId为文件id</span>
    <span $query</span> = pg_query(<span $dbconn</span>, <span $selectStr</span><span );
    </span><span while</span>(<span $row</span> = pg_fetch_array(<span $query</span>, <span null</span>,<span  PGSQL_ASSOC)){
        </span><span $escapeBytea</span> = <span $row</span>['contents'<span ];
        </span><span $fileContents</span> = pg_unescape_bytea(<span $escapeBytea</span>); <span //</span><span 获得二进制数据</span>
        <span file_put_contents</span>(<span $fileName</span>, <span $fileContents</span>); <span //</span><span $fileName为带有后缀名的文件名,如hello.pdf</span>
<span }
</span>?>
Copy after login

   导出文件后,如果二进制数据转码错误就会出现文件打不开的现象,比如错误的PDF文件打开时弹出错误如下图:

  这种错误在数据库迁移时特别容易出现(本人是从PostgreSQL 8.4迁移到9.1),解决的方法是修改PostgreSQL的配置文件
postgresql.conf,将bytea_output的输出类型设置为转义类型(escape)输出,即bytea_output = 'escape'(如果前面有#,删除开启配置),然后reload一下PostgreSQL的配置使修改生效,这样二进制数据就可以正常解码并输出到文件。 

 

博客声明:

  本博客中的所有文章,除标题中注明“转载”字样外,其余所有文章均为本人原创或在查阅资料后总结完成,引用非转载文章时请注明此声明。—— 博客园-pallee

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template