Thread: files, php & pgsql
I've been thinking the same that the list is dead (or my mail was stuffed) but now I see it's not the case :) I do have a problem (!!), and its trying to get files (of any type) to work with pgsql & php. I'm using pgsql 7.0.2, php4. Anyone have any hints? Chris. Here's the form I use to upload the files.. <form enctype="multipart/form-data" name="fileinsert" action="file-insert.php" method="post"> <input type="hidden" name="max_file_size" value="10000"> <input type="file" size="35" name="userfile"><BR> <input type="submit" value="Upload It" name="uploadfile"> I upload the files like this.... <?php $db=pg_connect("","","","","images"); $fp=fopen("$userfile","r"); pg_exec ($db,"begin"); $loid = pg_locreate($db); $lofp = pg_loopen($db, $loid, "w"); while ($nbytes = fread ($fp, BUFSIZE)) { $tmp = pg_lowrite($lofp, $nbytes); if ($tmp < $nbytes) echo "Error writing the file"; } $sql = "insert into images (picnum, image) values (nextval('images_picnum_seq'), '$loid')"; pg_exec($db,$sql); pg_loclose($lofp); pg_exec($db, "commit"); fclose($fp); echo "done"; pg_close($db); ?> I list the files like this.. $db = pg_connect("","","","","images"); $sql = "select * from images"; $result = pg_exec($db, $sql); if (!$result) {printf ("error"); } $totalrows = pg_numrows($result); $row=0; do { $myrow = pg_fetch_object($result,$row); $image_oid = $myrow->picnum; echo "this is my image <img src=\"blob2jpg.php?image=$image_oid\" border=0><br>"; $row++; }while ($row < $totalrows); echo $pic; ?> I (try to) display the files like this.... <? $db=pg_connect("","","","","images"); $sql="select * from images where picnum=$picnum"; $result=pg_exec($db,$sql); if (!$result) { printf ("error"); } Header("Content-type: image/gif"); $myrow = pg_fetch_object($result,0); $image = $myrow->image; pg_loreadall($image); pg_close($db); ?> and finally my database.. images=# \d images Table "images" Attribute | Type | Modifier -----------+---------+------------------------------- picnum | integer | not null default...... image | oid |
Chris, I ran a quick test and got the following error: "PostgresSQL query failed: ERROR: parser: parse error at or near " " If the following is how the code is actually formated: >$sql = "insert into images (picnum, image) values >(nextval('images_picnum_seq'), '$loid')"; it might be the source of the problem. Putting the quoted string all on one line as in: $sql = "insert into images (picnum, image) values (nextval('images_picnum_seq'),'$loid')"; eliminates this error. Hope this helps . . . Cheers, Dave
Hey, <snip> >Putting the quoted string all on one line as in: > >$sql = "insert into images (picnum, image) values >(nextval('images_picnum_seq'),'$loid')"; I think that's how I've got it, it might've just been bad line wrapping. My main problem is with the retrieval of files from the database :/ Thanks anyway :) Chris.
Ahhh . . . Guess this is likely not to be the problem, as it is clear that it's the formatting of the message that breaks the line. Oops. I'll let you know if I discover anything else. Dave At 08:45 PM 10/18/00 +1000, you wrote: >I've been thinking the same that the list is dead (or my mail was stuffed) >but now I see it's not the case :) > >I do have a problem (!!), and its trying to get files (of any type) to work >with pgsql & php. I'm using pgsql 7.0.2, php4. > >Anyone have any hints? > >Chris. > >Here's the form I use to upload the files.. ><form enctype="multipart/form-data" name="fileinsert" >action="file-insert.php" method="post"> ><input type="hidden" name="max_file_size" value="10000"> ><input type="file" size="35" name="userfile"><BR> ><input type="submit" value="Upload It" name="uploadfile"> > >I upload the files like this.... ><?php >$db=pg_connect("","","","","images"); >$fp=fopen("$userfile","r"); >pg_exec ($db,"begin"); >$loid = pg_locreate($db); >$lofp = pg_loopen($db, $loid, "w"); >while ($nbytes = fread ($fp, BUFSIZE)) >{ >$tmp = pg_lowrite($lofp, $nbytes); >if ($tmp < $nbytes) echo "Error writing the file"; >} >$sql = "insert into images (picnum, image) values >(nextval('images_picnum_seq'), '$loid')"; >pg_exec($db,$sql); >pg_loclose($lofp); >pg_exec($db, "commit"); >fclose($fp); >echo "done"; >pg_close($db); >?> > >I list the files like this.. >$db = pg_connect("","","","","images"); >$sql = "select * from images"; >$result = pg_exec($db, $sql); >if (!$result) {printf ("error"); } >$totalrows = pg_numrows($result); >$row=0; >do >{ >$myrow = pg_fetch_object($result,$row); >$image_oid = $myrow->picnum; >echo "this is my image <img src=\"blob2jpg.php?image=$image_oid\" >border=0><br>"; >$row++; >}while ($row < $totalrows); >echo $pic; >?> > >I (try to) display the files like this.... ><? >$db=pg_connect("","","","","images"); >$sql="select * from images where picnum=$picnum"; >$result=pg_exec($db,$sql); >if (!$result) { printf ("error"); } >Header("Content-type: image/gif"); >$myrow = pg_fetch_object($result,0); >$image = $myrow->image; >pg_loreadall($image); >pg_close($db); >?> > >and finally my database.. >images=# \d images > Table "images" > Attribute | Type | Modifier >-----------+---------+------------------------------- > picnum | integer | not null default...... > image | oid | > >
Do you get any type of error? Or does it just not do anything? Adam Lang Systems Engineer Rutgers Casualty Insurance Company ----- Original Message ----- From: "Chris" <tomcat@weirdness.com> To: <pgsql-php@hub.org> Sent: Wednesday, October 18, 2000 6:45 AM Subject: files, php & pgsql > I've been thinking the same that the list is dead (or my mail was stuffed) > but now I see it's not the case :) > > I do have a problem (!!), and its trying to get files (of any type) to work > with pgsql & php. I'm using pgsql 7.0.2, php4. > > Anyone have any hints? > > Chris. > > Here's the form I use to upload the files.. > <form enctype="multipart/form-data" name="fileinsert" > action="file-insert.php" method="post"> > <input type="hidden" name="max_file_size" value="10000"> > <input type="file" size="35" name="userfile"><BR> > <input type="submit" value="Upload It" name="uploadfile"> > > I upload the files like this.... > <?php > $db=pg_connect("","","","","images"); > $fp=fopen("$userfile","r"); > pg_exec ($db,"begin"); > $loid = pg_locreate($db); > $lofp = pg_loopen($db, $loid, "w"); > while ($nbytes = fread ($fp, BUFSIZE)) > { > $tmp = pg_lowrite($lofp, $nbytes); > if ($tmp < $nbytes) echo "Error writing the file"; > } > $sql = "insert into images (picnum, image) values > (nextval('images_picnum_seq'), '$loid')"; > pg_exec($db,$sql); > pg_loclose($lofp); > pg_exec($db, "commit"); > fclose($fp); > echo "done"; > pg_close($db); > ?> > > I list the files like this.. > $db = pg_connect("","","","","images"); > $sql = "select * from images"; > $result = pg_exec($db, $sql); > if (!$result) {printf ("error"); } > $totalrows = pg_numrows($result); > $row=0; > do > { > $myrow = pg_fetch_object($result,$row); > $image_oid = $myrow->picnum; > echo "this is my image <img src=\"blob2jpg.php?image=$image_oid\" > border=0><br>"; > $row++; > }while ($row < $totalrows); > echo $pic; > ?> > > I (try to) display the files like this.... > <? > $db=pg_connect("","","","","images"); > $sql="select * from images where picnum=$picnum"; > $result=pg_exec($db,$sql); > if (!$result) { printf ("error"); } > Header("Content-type: image/gif"); > $myrow = pg_fetch_object($result,0); > $image = $myrow->image; > pg_loreadall($image); > pg_close($db); > ?> > > and finally my database.. > images=# \d images > Table "images" > Attribute | Type | Modifier > -----------+---------+------------------------------- > picnum | integer | not null default...... > image | oid |
Chris, Try enclosing your call to pg_loreadall() in a transaction block as you do for pg_lowrite(). Without pg_exec("begin") . . . pg_exec("commit") and pg_exec("end") PHP reports "0 is not a PostgreSQL large object index" when calling pg_loreadall(). Also, pg_loreadall() is looking for a "file descriptor" and it appears that you are passing it an 'oid'. I was able to get the following test to work on PostgreSQL 6.5 using PHP3. database name: images table name: images ______________________ field | type | -----------+---------+ picnum | serial | image | oid | ---------------------- <? $db=pg_connect("","","","","images"); pg_exec ($db,"begin"); $loid = pg_locreate($db); $lofp = pg_loopen($db, $loid, "rw"); $strWrite = "test"; pg_lowrite($lofp,$strWrite); $sql = "insert into images(picnum,image) values(nextval('images_picnum_seq'),'$loid')"; $res = pg_exec($db,$sql); pg_loclose($lofp); pg_exec($db, "commit"); pg_exec($db, "end"); $res = pg_exec($db,"SELECT * FROM images"); $row = pg_fetch_object($res,0); pg_exec ($db,"begin"); $lofp = pg_loopen($db, $row->image, "rw"); ?> <HTML> <BODY> <? pg_loreadall($lofp); pg_loclose($lofp); pg_exec($db, "commit"); pg_exec($db, "end"); pg_close($db); ?> </BODY> </HTML> Regards, Dave At 08:45 PM 10/18/00 +1000, you wrote: >I've been thinking the same that the list is dead (or my mail was stuffed) >but now I see it's not the case :) > >I do have a problem (!!), and its trying to get files (of any type) to work >with pgsql & php. I'm using pgsql 7.0.2, php4. > >Anyone have any hints? > >Chris. > >Here's the form I use to upload the files.. ><form enctype="multipart/form-data" name="fileinsert" >action="file-insert.php" method="post"> ><input type="hidden" name="max_file_size" value="10000"> ><input type="file" size="35" name="userfile"><BR> ><input type="submit" value="Upload It" name="uploadfile"> > >I upload the files like this.... ><?php >$db=pg_connect("","","","","images"); >$fp=fopen("$userfile","r"); >pg_exec ($db,"begin"); >$loid = pg_locreate($db); >$lofp = pg_loopen($db, $loid, "w"); >while ($nbytes = fread ($fp, BUFSIZE)) >{ >$tmp = pg_lowrite($lofp, $nbytes); >if ($tmp < $nbytes) echo "Error writing the file"; >} >$sql = "insert into images (picnum, image) values >(nextval('images_picnum_seq'), '$loid')"; >pg_exec($db,$sql); >pg_loclose($lofp); >pg_exec($db, "commit"); >fclose($fp); >echo "done"; >pg_close($db); >?> > >I list the files like this.. >$db = pg_connect("","","","","images"); >$sql = "select * from images"; >$result = pg_exec($db, $sql); >if (!$result) {printf ("error"); } >$totalrows = pg_numrows($result); >$row=0; >do >{ >$myrow = pg_fetch_object($result,$row); >$image_oid = $myrow->picnum; >echo "this is my image <img src=\"blob2jpg.php?image=$image_oid\" >border=0><br>"; >$row++; >}while ($row < $totalrows); >echo $pic; >?> > >I (try to) display the files like this.... ><? >$db=pg_connect("","","","","images"); >$sql="select * from images where picnum=$picnum"; >$result=pg_exec($db,$sql); >if (!$result) { printf ("error"); } >Header("Content-type: image/gif"); >$myrow = pg_fetch_object($result,0); >$image = $myrow->image; >pg_loreadall($image); >pg_close($db); >?> > >and finally my database.. >images=# \d images > Table "images" > Attribute | Type | Modifier >-----------+---------+------------------------------- > picnum | integer | not null default...... > image | oid | > >
>Do you get any type of error? Or does it just not do anything? No errors. I get a broken link / image. (ie nothing). I'll try Dave's other suggestion when I get home. Thanks. Chris.