Re: Storing images in postgreSQL (with PHP)



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)
.



Relevant Pages

  • Re: storing path only to database
    ... Do not store the images in the file, rather store them on the hard drive and ... store the path to them in the Access database. ... Alternatively, you can store images in multiple tables, each one of which is ... Dim MyFolder As String ...
    (microsoft.public.access.formscoding)
  • Re: Fastest Method to add rows to a dataset?
    ... I can tell you, from experience, that LINQ may load faster, but it will certainly bog down on putting thousands of rows into the database. ... I you might also consider putting up a temp table and creating a bulk load file. ... that produces data in a FOR statement i need somewhere to store it in memory ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Image problem
    ... This sounds like one of the symptoms of hitting the database size limit? ... Using Photo Editor to store JPEGs causes a huge overhead, since it stores each image uncompressed, then ... Either store the images externally as files, or as blobs if you want them in the database. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: [SLE] File Server
    ... >>store the images in the file system directly? ... > future if for some reason you are adding more weight images, ... Even in Oracle this is a really bad idea. ... before you store the body in the database. ...
    (SuSE)
  • Re: file upload storage - database vs folder
    ... Suppose I do store my images for a web site in a BLOB ... > I have used both techniques whilst working with oracle database. ...
    (microsoft.public.dotnet.general)