note 98792 added to function.pg-escape-bytea
| From: | Mike-RaWare at osu1 dot php dot net | Date: | Thu, 08 Jul 2010 09:03:33 +0000 |
| Subject: | note 98792 added to function.pg-escape-bytea | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170780@lists.php.net to get a copy of this message | ||
To prevent any problems with encoding you could use hexadecimal or base64 input to save and retrieve
data to the database:
<?php
// Connect to the database
$dbconn = pg_connect( 'dbname=foo' );
// Read in a binary file
$data = file_get_contents( 'image1.jpg' );
// Escape the binary data
$escaped = bin2hex( $data );
// Insert it into the database
pg_query( "INSERT INTO gallery (name, data) VALUES ('Pine trees',
decode('{$escaped}' , 'hex'))" );
// Get the bytea data
$res = pg_query("SELECT encode(data, 'base64') AS data FROM gallery WHERE
name='Pine trees'");
$raw = pg_fetch_result($res, 'data');
// Convert to binary and send to the browser
header('Content-type: image/jpeg');
echo base64_decode($raw);
?>
----
Server IP: 83.137.20.135
Probable Submitter: 83.119.213.156
----
Manual Page -- http://www.php.net/manual/en/function.pg-escape-bytea.php
Edit -- https://master.php.net/note/edit/98792
Del: integrated -- https://master.php.net/note/delete/98792/integrated
Del: useless -- https://master.php.net/note/delete/98792/useless
Del: bad code -- https://master.php.net/note/delete/98792/bad+code
Del: spam -- https://master.php.net/note/delete/98792/spam
Del: non-english -- https://master.php.net/note/delete/98792/non-english
Del: in docs -- https://master.php.net/note/delete/98792/in+docs
Del: other reasons-- https://master.php.net/note/delete/98792
Reject -- https://master.php.net/note/reject/98792
Search -- https://master.php.net/manage/user-notes.php