Thread: Displaying image from php script displays string

Displaying image from php script displays string

From
Alexander Reichstadt
Date:
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:



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



Re: Displaying image from php script displays string

From
John R Pierce
Date:
On 09/11/12 6:24 AM, Alexander Reichstadt wrote:
> What am I doing wrong?
>

I'm not a php programmer (hate the sloppy stuff) but nowhere do I see
you specifying a mime image type for your image data being returned to
the browser.

this is a php problem, not a postgres problem.

--
john r pierce                            N 37, W 122
santa cruz ca                         mid-left coast



Re: Displaying image from php script displays string

From
Raymond O'Donnell
Date:
On 11/09/2012 14:24, Alexander Reichstadt wrote:
>
>
> What am I doing wrong?
>

What version of PG are you using? The default output format of bytea
changed from "escape" to "hex" a version or two ago - try changing it to
"escape" and see if that makes a difference:

  ALTER DATABASE xxxxx SET bytea_output='escape';

BTW, it's a REALLY bad idea to build literal SQL queries from input
values, as you're doing - you should use parameters and
pg_query_params() instead.

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie


Re: Displaying image from php script displays string

From
Raymond O'Donnell
Date:
On 11/09/2012 20:42, Alexander Reichstadt wrote:
> Thanks for the push, I found the information I needed. It works now.

Glad you got sorted. For people looking at the archives, what was the
problem, and how did you fix it?

Ray.


--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie


Re: Displaying image from php script displays string

From
Jasen Betts
Date:
On 2012-09-11, Raymond O'Donnell <rod@iol.ie> wrote:
>
> BTW, it's a REALLY bad idea to build literal SQL queries from input
> values, as you're doing - you should use parameters and
> pg_query_params() instead.

Although (still) marked "experimental"
pg_insert and pg_update work really well
the more recent pg_query_params still seems kind of dodgy.

--
⚂⚃ 100% natural