OID output problems

Started by surfer girlabout 26 years ago9 messagesgeneral
Jump to latest
#1surfer girl
gidget@getitgear.com

Hello,

I am trying to write a program that will input and output images from a Postgres database using PHP.

The inputting seems to be going fine - well, it seems to be fine, since I can't test it because it's the output I'm stuck on.

What I've got so far is:

<?PHP
Header("Content-type: image/gif");
(DATABASE CONNECT STUFF HERE)
pg_exec ($conn, "BEGIN");
$result = pg_Exec($conn, "SELECT file FROM file WHERE key_fileid = '7'");
$oid = pg_Result($result, 0, "file");
$handle = pg_loopen($conn, $oid, "r");
pg_loreadall($handle);
pg_exec ($conn, "COMMIT");
?>

What this does is print up a broken image in the browser. If you view the source it will provide a name of a temp file, i.e., /tmp/php08543baa

Am I missing something here? Or is the temp file not being written to properly or something?

Thanks in advance

_____________________________________________________________
Get It Gear --> http://www.getitgear.com

#2Karel Zak
zakkr@zf.jcu.cz
In reply to: surfer girl (#1)
Re: OID output problems

On Tue, 2 May 2000, surfer girl wrote:

Hello,

I am trying to write a program that will input and output images from a Postgres database using PHP.

The inputting seems to be going fine - well, it seems to be fine, since I can't test it because it's the output I'm stuck on.

What I've got so far is:

<?PHP
Header("Content-type: image/gif");
(DATABASE CONNECT STUFF HERE)
pg_exec ($conn, "BEGIN");
$result = pg_Exec($conn, "SELECT file FROM file WHERE key_fileid = '7'");
$oid = pg_Result($result, 0, "file");
$handle = pg_loopen($conn, $oid, "r");
pg_loreadall($handle);
pg_exec ($conn, "COMMIT");
?>

What this does is print up a broken image in the browser.

I known this proble. You are probably right. A problem (IMHO) is in PHP.

Try use insdead pg_readall() this code with pg_loread():

$data = pg_loread($handle, 5000000 /* or a exactly size */);
echo $data;

Karel

#3Robert B. Easter
reaster@comptechnews.com
In reply to: surfer girl (#1)
Re: OID output problems

On Tue, 02 May 2000, surfer girl wrote:

Hello,

I am trying to write a program that will input and output images from a Postgres database using PHP.

I made a very simple display image routine as follows:

-------------------------------------------------------------------------------
<?php
/*
Takes arg: imageid=oid of image
*/

$conn = pg_pconnect("user=username dbname=databasename");

pg_exec($conn, "begin");
@ $fd = pg_loopen($conn, $imageid, "r");
if($fd) {
$hdr = pg_loread($fd, 4);
switch($hdr) {
case "\xFF\xD8\xFF\xE0":
Header( "Content-type: image/jpeg" );
break;
case "GIF8":
Header( "Content-type: image/gif" );
break;
case "\x89PNG":
Header( "Content-type: image/png" );
break;
}
echo $hdr;
pg_loreadall($fd);
pg_exec($conn, "end");
}
else {
echo "Query rejected.";
}

pg_close($conn);
?>
-------------------------------------------------------------------------------
--
Robert B. Easter
reaster@comptechnews.com

#4Robert B. Easter
reaster@comptechnews.com
In reply to: Robert B. Easter (#3)
Re: OID output problems

On Tue, 02 May 2000, surfer girl wrote:

Hi Robert,

Thanks for the code. I tried the code, and it works fine (no errors) except instead of getting an image output, it outputs "/tmp/php08543baa" to the browser or something similar (just like my code). Am I missing something where I should be transferring a temp file to something web-readable - or is my Postgres doing something funny? Sorry if this sounds dense but I've followed a number of examples and I thought the actual image was supposed to be sent directly to the browser using pg_loreadall.

Try recompiling php 4.0RC1 or whatever is current with the latest Apache
source (1.3.12). I'm thinking maybe you are not running php as compiled into
the server.

You can install Apache first as normal and make sure that all of its bin
files are on the PATH so that apxs is available. Then compile php (this is how
I did it):

./configure --with-apxs --with-pgsql --without-mysql --with-xml
--enable-track-vars
make
make install

Then check your httpd.conf file:
LoadModule php4_module libexec/libphp4.so
AddModule mod_php4.c
DirectoryIndex index.html index.php
AddType application/x-httpd-php .php
AddType application/x-httpd-source .phps

Good luck.

#5surfer girl
gidget@getitgear.com
In reply to: Robert B. Easter (#4)
Re: OID output problems

Hi Robert,

Thanks for the code. I tried the code, and it works fine (no errors) except instead of getting an image output, it outputs "/tmp/php08543baa" to the browser or something similar (just like my code). Am I missing something where I should be transferring a temp file to something web-readable - or is my Postgres doing something funny? Sorry if this sounds dense but I've followed a number of examples and I thought the actual image was supposed to be sent directly to the browser using pg_loreadall.

Thanks again!

--- "Robert B. Easter" <reaster@comptechnews.com> wrote:

<?php
/*
Takes arg: imageid=oid of image
*/

$conn = pg_pconnect("user=username dbname=databasename");

pg_exec($conn, "begin");
@ $fd = pg_loopen($conn, $imageid, "r");
if($fd) {
$hdr = pg_loread($fd, 4);
switch($hdr) {
case "\xFF\xD8\xFF\xE0":
Header( "Content-type: image/jpeg" );
break;
case "GIF8":
Header( "Content-type: image/gif" );
break;
case "\x89PNG":
Header( "Content-type: image/png" );
break;
}
echo $hdr;
pg_loreadall($fd);
pg_exec($conn, "end");
}
else {
echo "Query rejected.";
}

pg_close($conn);
?>
-------------------------------------------------------------------------------
--
Robert B. Easter
reaster@comptechnews.com

_____________________________________________________________
Get YourName@getitgear.com email Today!
Visit http://mail.getitgear.com

#6surfer girl
gidget@getitgear.com
In reply to: surfer girl (#5)
Re: OID output problems
--- "Robert B. Easter" <reaster@comptechnews.com> wrote:

Try recompiling php 4.0RC1 or whatever is current with the latest Apache
source (1.3.12). I'm thinking maybe you are not running php as compiled into
the server.

Thanks - I had PHP compiled into the server - though I may try the recompile as a last resort (before the major last resort of just keeping images as files and putting the filename and location into the db).

Someone asked if my INPUT was correct. Here's what I've got:

pg_Exec($conn, "BEGIN");
$oid = pg_locreate($conn);
$handle = pg_loopen($conn, $oid, "w");
pg_lowrite($handle, $file);
pg_loclose($handle);
$result = pg_Exec($conn, "INSERT INTO file (file, filetype, permissions, key_imageid) VALUES ('$oid', '$filetype','$permissions','$imageid')" );
pg_Exec($conn, "COMMIT");

Unfortunately there aren't tons of examples around for this stuff so I don't have much to compare this to. (Based this off the Addison Wesley book).

Thanks

_____________________________________________________________
Get It Gear --> http://www.getitgear.com

#7Ross J. Reedstrom
reedstrm@rice.edu
In reply to: surfer girl (#6)
Re: OID output problems

On Tue, May 02, 2000 at 10:13:14PM -0800, surfer girl wrote:

--- "Robert B. Easter" <reaster@comptechnews.com> wrote:

Try recompiling php 4.0RC1 or whatever is current with the latest Apache
source (1.3.12). I'm thinking maybe you are not running php as compiled into
the server.

Thanks - I had PHP compiled into the server - though I may try the recompile as a last resort (before the major last resort of just keeping images as files and putting the filename and location into the db).

Someone asked if my INPUT was correct. Here's what I've got:

pg_Exec($conn, "BEGIN");
$oid = pg_locreate($conn);
$handle = pg_loopen($conn, $oid, "w");
pg_lowrite($handle, $file);

Hmm, based on my reading of the php4 docs, this will right the contents of the variable
'file' to the lo, expecting it to be a null terminated string. I'm not sure how you're
supposed to get binary data in there. Is 'file' by any chance, the name of your file,
not the contents?

Ross
--
Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005

#8surfer girl
gidget@getitgear.com
In reply to: Ross J. Reedstrom (#7)
Re: OID output problems

The documentation on this is so scarse it's hard to figure out what the right format is supposed to be for all this. After much searching (websites, various mailing list archives), I found the "note" that PHP has a special variable "userfile" for file uploads. This I have changed, and a straight upload to a file works OK, but the Postgres input does not. "$userfile" actually points to a file such as "/tmp/php12595baa" - now, my question is, how do I get the actual BINARY FILE into the database, and not the NAME of the TEMP FILE. (This, at least, explains the bizarre output).

What I had put for my input was taken out of the Addison Wesley book but the explanation was not enough and frankly it's not working. I have yet to find another example of how to do this, and the function reference is cryptic at best.

Any ideas would be absolutely appreciated. Thanks.

--- "Ross J. Reedstrom" <reedstrm@wallace.ece.rice.edu>

wrote:

pg_Exec($conn, "BEGIN");
$oid = pg_locreate($conn);
$handle = pg_loopen($conn, $oid, "w");
pg_lowrite($handle, $file);

Hmm, based on my reading of the php4 docs, this will right the contents of the variable
'file' to the lo, expecting it to be a null terminated string. I'm not sure how you're
supposed to get binary data in there. Is 'file' by any chance, the name of your file,
not the contents?

Ross
--
Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005

_____________________________________________________________
Get It Gear --> http://www.getitgear.com

#9Ron Chmara
ron@Opus1.COM
In reply to: surfer girl (#8)
Re: OID output problems

surfer girl wrote:

The documentation on this is so scarse it's hard to figure out what the right format is supposed to be for all this. After much searching (websites, various mailing list archives), I found the "note" that PHP has a special variable "userfile" for file uploads.

It has a few.
/*
echo "<P>Original file name is <B>$userfile_name</B>\n";
echo "File size is <B>$userfile_size</B>\n";
echo "File type is <B>$userfile_type</B>\n";
echo "Temporary file name is <B>$userfile</B></font>\n";
*/

This I have changed, and a straight upload to a file works OK, but the Postgres input does not. "$userfile" actually points to a file such as "/tmp/php12595baa" - now, my question is, how do I get the actual BINARY FILE into the database, and not the NAME of the TEMP FILE. (This, at least, explains the bizarre output).

What I had put for my input was taken out of the Addison Wesley book but the explanation was not enough and frankly it's not working. I have yet to find another example of how to do this, and the function reference is cryptic at best.

Any ideas would be absolutely appreciated. Thanks.

--- "Ross J. Reedstrom" <reedstrm@wallace.ece.rice.edu>

wrote:

pg_Exec($conn, "BEGIN");
$oid = pg_locreate($conn);
$handle = pg_loopen($conn, $oid, "w");
pg_lowrite($handle, $file);

Hmm, based on my reading of the php4 docs, this will write the contents of the variable
'file' to the lo, expecting it to be a null terminated string. I'm not sure how you're
supposed to get binary data in there. Is 'file' by any chance, the name of your file,
not the contents?

PHP doesn't really have an "open this file into memory" function, but it _does_ allow
you to burst lines of a file in, with fgets/fputs, or to build an array out
of the file contents. So, you could run a loop (metacoded, not yet tested):
if($filetowrite = fopen ("$userfile", "rb") //rb is read binary, right?
{
while (!feof($userfile)) //as long as it's not end of file
{
$stringtowrite = fgets($userfile); //get a string
pg_lowrite($handle, $file); //write that string
/* Offhand, I have no idea how fgets will handle the chars in your */
/* file. It's supposed to read to a newline, so you might have to */
/* Put one back in, a "/n" in the string.... if your BLOB, however,*/
/* has no lines, well that's another problem. */
}
}

All this being said, a frequent recommendation from the PHP list is to store the
file, on your filesystem, in the filesystem database, outside of an additional
database layer (yes, filesystems are databases). Combining this recommendation
with the limits imposed by PostgreSQL on row size, it's fairly impractical to
be doubling up on the work to write into a postgres BLOB, rather than having
BLOB storage outside the DB itself, with DB pointing to it (via filename). Of
course, you loose the ability for the DB engine to search _within_ the BLOB...

HTH,
-Bop

--
Brought to you from boop!, the dual boot Linux/Win95 Compaq Presario 1625
laptop, currently running RedHat 6.1. Your bopping may vary.