How to use the bytea field of PostgreSQL to store and read files and handle errors _ PHP Tutorial

Source: Internet
Author: User
Use the bytea field of PostgreSQL to store and handle read errors. The bytea field type in PostgreSQL can store data in binary format. the advantage of this is that the files originally stored in the website directory can be stored in the database, the disadvantage is that the bytea field type in PostgreSQL can store data in binary format. the advantage of this is that the files originally stored in the website directory can be stored in the database, the disadvantage is that if there are too many files or too many files, the data volume in the database will be greatly increased. backup and recovery will waste a lot of time, and data may also fail. I personally think that this storage method is very convenient when the file volume is small.

To put it bluntly, the following describes how to use the bytea field to store and read files. First, the file is stored in the bytea field, and the pg_escape_bytea method in PHP is mainly used. the code is as follows:

 

Restoring a file from the bytea field uses the pg_unescape_bytea method in PHP. the implementation code is as follows:

 

After the file is exported, if the binary data is incorrectly transcoded, the file cannot be opened. for example, an error occurs when the wrong PDF file is opened, for example:

This kind of error is especially prone to occur during database migration (I migrated from PostgreSQL 8.4 to 9.1). The solution is to modify the configuration file of PostgreSQL.
Postgresql. conf, set the output type of bytea_output to the escape type (escape) output, that is, bytea_output = 'escape '(If there is # above, delete and enable the configuration ), then reload the PostgreSQL configuration to make the modification take effect, so that the binary data can be decoded and output to the file.

Blog statement:

All the articles in this blog, except the words "reprinted" in the title, are original articles or summarized after reading the materials. please note this statement when referencing non-reprinted articles. -- Blog garden-pallee

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.