473,503 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing images in postgreSQL (with PHP)

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

Aug 9 '05 #1
3 10565
bi******@yahoo.co.uk 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)
Aug 9 '05 #2
> $base64 = base64_encode($binary);

Is this a similar idea to using pg_escape_bytea? Is the above better in
any way?

Aug 16 '05 #3
bi******@yahoo.co.uk wrote:
$base64 = base64_encode($binary);


Is this a similar idea to using pg_escape_bytea? Is the above better in
any way?


Yes, it is similar.

The advantage that the bytea type has, and the corresponding function
pg_escape_bytea, is that the storage requirements are only 75% of what is
required for the base64-encoded type.

However, the bytea-encoded data must be decoded when you get it back from
the server, and if you are going to attach it to an email you have to
base64 encode it, so it gets handled twice going out to a browser or email
program. So I pay the price in storage to save the base64 to disk so I can
read it off the disk and send it over the wire without processing it at
all.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Aug 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2855
by: mPath Records | last post by:
Hello, 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...
3
11737
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
7
3472
by: Benoit St-Jean | last post by:
I am looking at options/ways to store 12 million gif/jpg images in a database. Either we store a link to the file or we store the image itself in the database. Images will range from 4k to 35k in...
6
3038
by: bissatch | last post by:
Hi, I am currently writing a news admin system. I would like to add the ability to add images to each article. What I have always done in the past is uploaded (using a form) the image to a...
2
9275
by: bissatch | last post by:
Hi, I am trying to write script that is run when a form is submitted. The form contains an image input field and when submitted, the image is uploaded, resized and added as binary information to...
4
2713
by: Rednelle | last post by:
Greetings all, As a newbie, using Access 2000, I would appreciate advice on the best way to include pictures. I have developed a 'Home Inventory' database which can include jpeg thumbnails of...
7
2757
by: C G | last post by:
Dear All, What's the best way to store jpgs in postgresql to use in a web page? I tried to use large objects, but how would you extract them from a table to be viewed in a web-page without...
0
2685
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.