473,320 Members | 1,841 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,320 software developers and data experts.

Uploading binary files to postgreSQL from PHP

Hi team,

Its me again. I am still in my quest in studying php.

I just want to ask if you can upload binary files (ex. jpg, doc files) to the postgreSQL server through PHP? If so, how must this be done? Just a link on any tutorial will do. Thanks!
Jun 7 '07 #1
5 6208
pbmods
5,821 Expert 4TB
You can use file_get_contents to read the contents of the uploaded file into a string. Then addslashes and insert into your query.

Alternatively, you can store your files elsewhere on the server and then save the path in the database.
Jun 7 '07 #2
You can use file_get_contents to read the contents of the uploaded file into a string. Then addslashes and insert into your query.

Alternatively, you can store your files elsewhere on the server and then save the path in the database.
Hi pbmods,

Thanks for your reply. I have one more question. What is the difference between addslashes and pg_escape_bytea? Are they 2 different things?
Jun 7 '07 #3
pbmods
5,821 Expert 4TB
I have one more question. What is the difference between addslashes and pg_escape_bytea? Are they 2 different things?
Hrm.... Looks like pg_escape_bytea is designed for PostgreSQL :)
Jun 7 '07 #4
Hrm.... Looks like pg_escape_bytea is designed for PostgreSQL :)
Ohh.. hmm.. so which should I use? I tried to work this code out but failed. I patterned it over the example with a few minor changes :(

<?php
$data = file_get_contents($_FILES['form_data']['tmp_name']);
$escaped = pg_escape_bytea($data);
$result = pg_prepare($connection, "ins_pic",'INSERT INTO tbl_photo (id,data) VALUES ($1,$2)');
$result = pg_execute ($connection,"ins_pic",array('1','$escaped'));
?>

This is the page where I was trying to load the file from:
<form method="post" action="process.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<input type="hidden" name="task" value ="uplImg"/>
<input type="hidden" name="task" value ="fileUpload"/>
<p><input type="submit" name="submit" value="submit">
</form>

Many thanks for your replies
Jun 7 '07 #5
Hi Again,

I managed to make it work. I cant remember how but I found out the file is already in the db :))

Now I have to figure out how to retrive it.. he..he
Jun 7 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Marc | last post by:
Hello, I have a page where i can upload binary file (using the HTML input type=file approach). This works fine for relatively small files (<6MB)) but when files get bigger (13MB) there is a...
0
by: dermoon | last post by:
Made this simple cgi-upload script that uses fieldstorage from a web-form. ASCII-based files works fine, but if i try to upload binary-files (word-documents, images) the files seems to be truncated...
5
by: Kevin Ollivier | last post by:
Hi all, I've come across a problem that has me stumped, and I thought I'd send a message to the gurus to see if this makes sense to anyone else. =) Basically, I'm trying to upload a series of...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
0
by: Nikolay Petrov | last post by:
I need to upload big files to a web service. At client side I use the FileInput contol to get the local filename and then a binary reader to get file contents as byte array, then send this byte...
4
by: Holger Marzen | last post by:
Hi all, AFAIK it is possible for columns to be very large, up to about 2 GB. Are there any hints or experiences about storing binary data (jpg-images, pdf-documents) in PostgrreSQL with or...
3
by: Carlos | last post by:
Hello Forum, I would appreciate it if you could recommend settings to use auto-vacuum in my version 7.4 database. I am uploading several thousands records in the database at a rate of ~1 second...
4
by: Dmitry Teslenko | last post by:
Hello! I have some postgresql database that stores binary data (windows/linux executables). Field type I use for this is bytea. Also I have php script that allows users download these binaries via...
2
by: matech | last post by:
I have a problem with uploading special characters from excel files to mysql 5. It doesn't matter if I use UTF-8 or iso-8859-1 when uploading the trademark ™ symbol. htmlspecialchars() or...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.