By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,726 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,726 IT Pros & Developers. It's quick & easy.

Images, bytea and pg_escape_bytea, pg_unescape_bytea

P: n/a

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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.