473,606 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing uploaded image in PostgreSQL as a binary 'bytea' type

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 a db table. Please
note, I am using a PostgreSQL database

I have written all the code out below that deals with the submission
processing:
$tmpfilesize = $_FILES['imgfile']['size'];
$tmpfilename = $_FILES['imgfile']['tmp_name'];
$tmpfiletype = $_FILES['imgfile']['type'];

if ($tmpfilesize > 0) {

//check this is an image by using the file type info
if (substr($tmpfil etype, 0, 6) == 'image/') {

//create image from uploaded image
switch ($tmpfiletype)
{
case "image/jpeg":
case "image/pjpeg":
$img = imagecreatefrom jpeg($tmpfilena me);
break;
case "image/gif":
$img = imagecreatefrom gif($tmpfilenam e);
break;
case "image/png":
$img = imagecreatefrom png($tmpfilenam e);
break;
}

//resize image
$imginfo = getimagesize($t mpfilename);
$width = $imginfo[0];
$height = $imginfo[1];
$maxsize = 200;
if (($width > $maxsize) || ($height > $maxsize)) {
$ratio = max($width, $height) / $maxsize;
$newwidth = floor($width / $ratio);
$newheight = floor($height / $ratio);
$newimg = imagecreatetrue color($newwidth , $newheight);
imagecopyresamp led($newimg, $img, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
$img = $newimg;
}

//prepare image for database
ob_start();
imagejpeg($img, '', 80);
$imgdata = pg_escape_strin g(ob_get_conten ts());
ob_end_clean();

//write to db
$curdate = date("Y-m-d H:i:s");
$insert = "INSERT INTO imglib (createdon, imgdata) VALUES ('" .
$curdate . "', '" . $imgdata . "')";
pg_exec($db, $insert);
I have included all the code just encase but the only line that is
giving any problems is:
pg_exec($db, $insert);
I get the following error:
Warning: pg_exec(): Query failed: ERROR: unterminated quoted string at
or near "'ÿØÿà" at character 89 in
/data/httpd/VirtualHosts/webdev/htdocs/mb_sandbox/_test/img/simplebytea.php
on line 89
If it helps also, I used the following SQL to CREATE the DB table:
CREATE TABLE imglib (imgid serial, createdon timestamp, imgdata bytea);
I am very new to storing images. When I comment out the pg_exec() it
doesnt give me any problems which suggests that everything goes well
(apart from the fact that it doesnt write to db). Im guessing that the
fault is in the preperation of the image data prior to be written to
db. Is bytea the correct data type for this? I have successfully stored
an image as a BLOB to MySQL and the code above is a slightly modified
version of that code. I dont want to convert the code to a string of
byte64 as this will creates uneccessary overhead.

Any suggestions? Cheers

Burnsy

Aug 22 '05 #1
2 9295
Search this newsgroup first.
If memory serves me well, this very question has been on this ng a few times
the last few weeks alone.

Tip: Use google discussiongroup search capabilities.

Regards,
Erwin Moller
Aug 22 '05 #2
Long question, short answer...

The problem was that I was using pg_escape_strin g() instead of
pg_escape_bytea (). I got pg_escape_bytea () mistaken for something that
'escaped' the format from a bytea type to something else (like a
string) or is that still what it is doing? Works now anyway.

Burnsy

Aug 23 '05 #3

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

Similar topics

3
11752
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
6
3039
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 folder on the server and then in the database table that I INSERT the news article, I'll store the path of the uploaded image. To me this seems a bad idea as if the image paths were changed on the
3
10610
by: bissatch | last post by:
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...
1
8147
by: Josué Maldonado | last post by:
Hello list, That's the question, what is the equivalent data type of the msSQL image data type ? TIA, -- Josué Maldonado.
7
2770
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 having to write them to a scratch file somewhere first? Thanks
1
1911
by: Együd Csaba | last post by:
Hi All, I use PG 7.3.2 an 7.4.1 on RH 7.1. I'd like to store small (~ 100x100 px jpeg, or gif) images in a field in binary format. I tried to understand the documentation of the large objects but I'm afraid I coluldn't. I found some functions to import and export binary files into the db, but these functions work on the server side. How can I insert a block of binary data into a given field form the client (WinXP, pgsqlODBC 7.03.01)?...
2
4998
by: Eduardo Pérez Ureta | last post by:
I was wondering what the best way is to store a file hash (MD5 or SHA1) and make it primary key indexed. I have seen some people storing the hexadecimal encoded MD5 in a CHAR(32) but it may be a better idea to use a CHAR(16) without encoding the string, but that may cause some problems. What do you recommend? Do you have any experiences storing file hashes in a database? Do you know any good opensource software that stores file hashes...
0
2692
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 works for files larger than 8000 Bytes. If a file is less than 1000 Bytes I get the following message: Error message: --invalid input syntax for type oid: "\074\077......";
0
1658
by: badaboum84 | last post by:
I have to migrate a current PostgreSQL database to MSSQL. For doing this task I uses Python to create script for doing this task. Now, I want to extract the data from the current PostgreSQL database, but I need a API. I decided to go with PSYCOPG2, but it seem to have a problem with the BYTEA datatype. I use these lines of code : self.cursor.execute('SELECT * FROM electronicassembly WHERE id_pk = 2') list = self.cursor.fetchall()...
0
8036
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7978
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8448
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8317
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6796
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5470
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2454
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 we have to send another system
0
1313
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.