473,223 Members | 1,772 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,223 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 10502
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.