Re: Storing images in postgreSQL (with PHP)
- From: Kenneth Downs <knode.wants.this@xxxxxxxxxxxx>
- Date: Tue, 09 Aug 2005 18:22:30 -0400
bissatch@xxxxxxxxxxx wrote:
> Hi,
>
> I am wanting to learn how to store images in a postgreSQL database. I
> have got as far as uploading the file using a file/browse field on an
> html form and have been able to catch the file using $_FILES. What I
> dont know is how to to them store the image in a database. Can someone
> please supply the code required to do this? Also, what is the SQL code
> needed to CREATE the database?
>
> I have in the past done this by storing the images in a file structure
> but would prefer to do it this way. I have tried looking on Google for
> some good tutorials but none of them appeared to explain exactly what
> was happening and how to do it. If anyone can recommend any good
> tutorials that would be great.
>
> Burnsy
There are two ways.
The first method is to use column type "bytea" and store the picture as
binary data.
The second method is to use column type "text", convert the file to base64,
and load it this way. This gives you a 33% hit on storage space, but IMHO
is far easier to deal with, and avoids messy binary stuff.
Some code to load a picture might be:
$binary = file_get_contents("somefile.jpg");
$base64 = base64_encode($binary);
pg_query("create tables pictures (picture text)");
pg_query("insert into pictures (picture) values ('$base64')");
There is of course a lot more too it, but hopefully this will get you
started.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
.
- Follow-Ups:
- Re: Storing images in postgreSQL (with PHP)
- From: bissatch
- Re: Storing images in postgreSQL (with PHP)
- References:
- Storing images in postgreSQL (with PHP)
- From: bissatch
- Storing images in postgreSQL (with PHP)
- Prev by Date: PHP newbie : exist short form for print() ?
- Next by Date: Re: Destroy $_POST vars after use - is it possible?
- Previous by thread: Storing images in postgreSQL (with PHP)
- Next by thread: Re: Storing images in postgreSQL (with PHP)
- Index(es):
Relevant Pages
|