update page now

Voting

: two minus zero?
(Example: nine)

The Note You're Voting On

Mike-RaWare
15 years ago
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);
?>

<< Back to user notes page

To Top