Hi all
this has taken a lot of my time and without result. I am hoping a helping hand from u guys. I want to store picture files in Postgres database. I am attaching all the files along with this mail. Please help me.
Lokking for your help
Sanjay
1.first the table structure.
Table "image"
Column | Type | Modifiers
-------------+---------------+-----------
id | integer | not null
description | character(50) |
image | oid |
filename | character(50) |
filesize | character(50) |
filetype | character(50) |
Primary key: image_pkey
1.This file image1.php loads picture files in the database.Dont worry about open1.php there is no probleb with connection.
<HTML>
<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
<BODY>
<?php
require_once("open1.php");
if ($submit) {
$conn = connection();
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
$sql = "INSERT INTO image (id,description,image,filename,filesize,filetype) ".
"VALUES(NEXTVAL('idseq'),'$form_description',lo_import('$data'),'$form_data_name','$form_data_size','$form_data
_type')";
$rs = $conn->execute($sql);
if(!$rs)print "Error";
echo "Image ";
print $form_data_name;echo " Of Size ";
print $form_data_size;echo " And type ";
print $form_data_type;echo " Is Inserted In The Database";
echo "You Described the Image As ";
print $form_description;
}
else {
?>
<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
File Description:<br>
<input type="text" name="form_description" size="40">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<p><input type="submit" name="submit" value="submit">
</form>
<?php
}
?>
</BODY>
</HTML>
- Files image2.php and image3.php retrive images from database.
image2.php->
<?php
$db = pg_connect("hostname portno dbname user");
if (!$db) {echo "An database connection error occurred.\n"; exit;}
$sql = "SELECT description,image FROM image";
$result = pg_exec($db, $sql);
$rowcount = 0;
while (list($description,$image) = @pg_fetch_array($result, $rowcount))
{
echo "<img src=\"image3.php?image=$image\">";
$rowcount++;
}
?>
image3.php->
<?php
$db = pg_pconnect("hostname portno dbname user");
pg_exec($db,"begin");
$fhandle = pg_loopen($db,$image,"r");
header("Content-type: image/pjpeg");
pg_loreadall($fhandle);
pg_loclose($fd);
pg_exec($db,"commit");
?>