PHP implements reading and writing documents or images in the bytea field of postgresql

Source: Internet
Author: User

It took nearly two days to upload the space-based documents in batches. The original document was originally stored in the folder under the website directory as a file, and then stored in the bytea field of PostgreSQL. This field is equivalent to the blob or clob field of oracle, data is stored in a second-level system, with a maximum of 2 GB of data.

I am not familiar with PHP programming. I have read and write the bytea FIELD IN THE postgesql database only after reading a large amount of reference materials. I would like to share with you the following:

First, you need to modify the php. ini file under the PHP installation directory to enable extension = php_pdo_pgsql.dll and extension = php_pgsql.dll to connect to the PostgreSQL database.

// Create a database connection
Function Createpgconnect ()
{
$ Host = " Localhost " ;
$ Port = " 5432 " ;
$ Dbname = " GLC " ;
$ User = " Postgres " ;
$ Password = " Gxsnprg2010 " ;
$ Dbconn = Pg_connect ( " Host = $ Host Port = $ Port Dbname = $ Dbname User = $ User Password = $ Password " );
Return   $ Dbconn ;
}

 

 

Key to writing document data into bytea FieldCode

$ Dbconn=Createpgconnect ();//Database Connection // Insert the file into the database tb_doc_res_data
$ Tmpfile = Iconv ( " UTF-8 " , " GBK " , $ Tmpfile ); // $ Tmpfile is the file name, which is garbled Chinese characters.
$ Data = File_get_contents (Upload_file_path . $ Tmpfile ); // Complete Document path
$ Escaped = Pg_escape_bytea ( $ Data ); // Key Points
$ Insertsql = " Insert into tb_doc_res_data values ( " . $ Tb_docmaxid . " ,'{ $ Escaped }') " ;
$ Result3 = Pg_query ( $ Dbconn , $ Insertsql ); // Execute the insert statement command

 

Read the bytea field value and save it as a PDF file

Code

// /Read the file from the database
Function Readblob ()
{
$ Dbconn = Createpgconnect (); // Database Connection
$ Insertsql = " Select filecontent from tb_doc_res_data where docid = 5 " ; // Query statement
$ Query = Pg_query ( $ Dbconn , $ Insertsql );
$ Row = Pg_fetch_result ( $ Query , ' Filecontent ' );

$ Filecontent = Pg_unescape_bytea ( $ Row ); // Obtain binary data
File_put_contents (Upload_file_path . ' 11. PDF ' , $ Filecontent ); // Convert binary data into PDF files
Return   " OK " ;
}

 

References:

Http://php.net/manual/en/function.pg-unescape-bytea.php

Http://www.phpf1.com/manual/pg-escape-bytea.html

Http://www.phpf1.com/manual/pg-unescape-bytea.html

Http://bytes.com/topic/php/answers/157633-storing-images-postgresql-php

Http://www.zephid.dk/2008/08/09/oid-vs-bytea-in-postgresql/

Http://www.zhougang.name /? P = 395

This blog statement: I have received support from sinosam during my technical exploration. In the future, all technical exploration achievements in my blog will be jointly owned by "no trace", "guoxin Si Nan", and "blog Park". Original works should be reprinted if necessary, please note this blog statement.

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.