_php tutorials for reading files and reading error problems using PostgreSQL Bytea word Gencun

Source: Internet
Author: User
The Bytea field type in PostgreSQL can store data in binary form, and the benefit is that files stored in a Web site directory can be stored in a database, and the downside is that if the file is too large, it can result in a large increase in the amount of data in the database. Backup and recovery can waste a lot of time, and data can be error-prone. Personally, in the case of small file size, the use of this storage method is still very convenient.

To the bottom, the following describes the use of Bytea Word Gencun read the file implementation method. The first is the file stored in the Bytea field method, the main use of PHP is the Pg_escape_bytea method, the code is as follows:

!--?   = Pg_connect ("host= ' localhost ' dbname= ' dbname ' user= ' user ' password= ' password ' port= ' Port '" span) or Span> (' Could not connect: '.   =  ("Utf-8", "GBK", );  =  (
             
               = Pg_escape_bytea ();   = "} ') "
                     
                      , 
                         
                          
                            
                    

The Pg_unescape_bytea method in PHP is used to restore files from the Bytea field, and the implementation code is as follows:

 = Pg_connect ("host= ' localhost ' dbname= ' dbname ' user= ' user ' password= ' password ' port= ' Port '"
   
     (' Could not connect: '.   = "". ;   = pg_query (, 
              
                ( = Pg_fetch_array ( , , = [' contents ' 
                       
                        = Pg_unescape_bytea ();   (,  ); 
                                
                                 ?> 
                                      
                      
               
   

After exporting the file, if the binary data transcoding error occurs when the file is not open, such as the wrong PDF file opens with an error such as:

This error is particularly likely to occur when the database is migrated (I migrated from PostgreSQL 8.4 to 9.1), and the workaround is to modify the PostgreSQL configuration file
postgresql.conf, set the output type of the bytea_output to escape type (escape) output, that is, bytea_output = ' escape ' (if the previous #, remove the open configuration), Then reload the configuration of PostgreSQL to make the changes take effect so that the binary data can be decoded and output to the file normally.

Blog statement:

All the articles in this blog, in addition to the title "reproduced" In the words, all the rest of the articles are my original or in the review of the information after the completion of the reference to non-reproduced articles please indicate this statement. --Blog Park-pallee

http://www.bkjia.com/PHPjc/440335.html www.bkjia.com true http://www.bkjia.com/PHPjc/440335.html techarticle The bytea field type in PostgreSQL can store data in binary form, and the advantage is that you can store files that are stored in a Web site directory in a database, the downside is ...

  • Related Article

    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.