From: | Alexander Reichstadt <lxr(at)mac(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Displaying image from php script displays string |
Date: | 2012-09-11 13:31:50 |
Message-ID: | 6BBC1F17-1F16-4C8B-9052-6480897860E8@mac.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Sorry if this has been asked a thousand time. I read through the web and think to have done everything I should.
I have a db with a bytea field. In it I stored data from a png file. When I stored it I used pg_escape_bytea(<CONTENTS OF FILE>) and it seems it ended up ok in the database.
I made a test setup, check this out:
http://qmprozesse.aemtervz.de/jdbqm/php/imagetest.html
Here the store script:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once 'classes/dbop.php';
$thePrefName = $_REQUEST['prefkeyname'];
//$theData = $_REQUEST['file'];
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
return;
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
$file=fopen($_FILES["file"]["tmp_name"],"r") or exit("Unable to open file!");
$filedata = '';
while (!feof($file))
{
$filedata .= fgetc($file);
}
fclose($file);
$filedata = pg_escape_bytea($filedata);
$query = 'INSERT INTO qmcustomerdata (prefkey,dataval) VALUES(\'' . $thePrefName . '\',\'' . $filedata . '\')';
Dbop::singleton()->querydb($query);
?>
Here the script customer_displayimage.php pointed to:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once 'classes/dbop.php';
$query = 'SELECT dataval FROM qmcustomerdata WHERE prefkey=\'' . $_REQUEST['prefkey'] . '\'';
$result = Dbop::singleton()->querydb($query);
echo pg_unescape_bytea($result[0]['dataval']);
?>
Here the Dbop and what it's doing:
public function querydb($querystring)
{
// echo '<br>DEBUG::' . $querystring . '<br>';
$dbconnection = pg_connect("host=localhost dbname=xxxxx user=xxxxx password=xxxxx")
or die('could not connect: ' . pg_last_error());
$result = pg_query($querystring);
$ra = array();
if (pg_num_rows($result)){
$ra = pg_fetch_all($result);
}
pg_free_result($result);
pg_close($dbconnection);
return $ra;
}
What am I doing wrong?
Thanks
Alex
From | Date | Subject | |
---|---|---|---|
Next Message | Adrian Klaver | 2012-09-11 13:49:27 | Re: Where do I get pgAdmin 1.16 for openSuSE? |
Previous Message | Alexander Reichstadt | 2012-09-11 13:24:57 | Displaying image from php script displays string |