471,595 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

Images, bytea and pg_escape_bytea, pg_unescape_bytea


I hope someone can help me with this problem - I'm getting nowhere
fast and have been working on it for a week.

All I'm trying to do is upload an image, store the image in a bytea
column of a postrgresql database, pull the image out of the datbase,
and verify that the image is still the same and it can be viewed by a
browser - that's all. The upload part works fine... I'm having
problems getting out what I put in. The file sizes (original image,
output image) never match and the browser is never happy.

Here's the basic setup:
PHP Version 4.3.2,
PostgreSQL 7.4.1 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.2.3
20030502 (Red Hat Linux 3.2.3-24)

I've tried about every possible combination of:

0. Upload the image no problem, browser can see it, and it works.

1. read from gif or jpeg file into string called $image

2. $bytea_image = pg_escape_bytea($image);

3. save $bytea_image in bytea column of postresql database

4. read $bytea_image back out from bytea column

5. $image = pg_unescape_bytea($bytea_image);

6. write $image back to disk

7. view image with browser - doesn't work.
pg_unescape_byte() doesn't seem to work at all - the string is always
truncated, maybe at a byte that's equal to NULL?
I've also tried:

5. $image = stripcslashes($bytea_image);
Better, but the output file is not the same size as the input file!!!
And, the output file does not render in a browser.
I've also tried at the Postgresql side doing the following:

5. SELECT encode(image_column::bytea, 'escape') FROM mytable LIMIT 1;

Still not correct...
So, here are my questions:
1. Does pg_unescape_bytea() actually work in PHP?
2. Is there a character set setting in Apache, Postgresql, or PHP that
has to be set for the escape and unescape to match?
3. Has anyone else had this problem?
I've found another way around this for now - I'm encoding the image as
hex, saving it, reading it back and converting it back to decimal and
it works no problem - but I'm obviously not using bytea as intended
and the image is now using two times the storage space to store an

Thanks for any help,

Jul 17 '05 #1
0 2640

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by bissatch | last post: by
2 posts views Thread by Alvar Freude | last post: by
reply views Thread by Alvar Freude | last post: by
4 posts views Thread by David Garamond | last post: by
7 posts views Thread by C G | last post: by
16 posts views Thread by Michal Hlavac | last post: by
7 posts views Thread by Dennis Gearon | last post: by
4 posts views Thread by Jerry LeVan | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.