Menu

#218 [PostgreSQL] Binary data escaping fails

3.3.3
open
PostgreSQL (42)
5
2017-02-21
2012-01-13
No

When inserting data into a bytea column from a binary file (e.g. a PNG image - using the "Insert" form, that creates a file input for such column), I get the following error:

ERROR: invalid byte sequence for encoding "UTF8": 0x89

Discussion

  • Cruxic

    Cruxic - 2015-03-03

    I get this error too using latest code from github (2015-03-03).

    Here's a work around:

    In the process_input() function (in functions.inc.php) you'll see where it handles bytea and other binary types. Instead of

    return q($file);
    

    do

    $file = base64_encode($file);
    return "decode('$file', 'base64')";
    

    This is postgresql specific of course. It wouldn't be too hard to make this work around proper by making q() take an optional "is_binary" parameter. Each driver could handle binary quoting in it's own way.

    Adminer is a great tool but, in my opinion, it handles binary data poorly. Binary data should be handled with prepared statements, not quoted like a string. The PHP docs on PDO::quote() say as much.

     
  • Jakub Vrána

    Jakub Vrána - 2017-02-21
    • summary: Binary data escaping fails --> [PostgreSQL] Binary data escaping fails
     

Log in to post a comment.