473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 10544
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
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
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
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
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
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
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
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.