By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Getting an image from a PHP source and ADOdb

P: n/a
Hi!

I am trying to display an image in my html document. The document contains
the following line:

<td class="Input"><img
src=getimage.php?tbl=dbo.Employees&type=jpg&col=Ph oto&idCol=EmployeeID&id=1
Border=0 alt=''>&nbsp;</td>
This call getimage.php source. Following is the php source itself:

<?PHP
include_once('ordersappdata.php');
$objConn1 = &ADONewConnection("mssql");
$objConn1->debug = $DebugMode;
$objConn1->Connect("server1","sa","sa2000","northwind");
include_once('utils.php');
$myQuote = "";
$objRSImage = "";
$strSQL = "";
$image = "";
$myType = "";

$handle = "";
$filename = 'test.txt';
$handle = fopen($filename, 'a');
//fwrite($handle, "Debug open");
//$myQuote = getQuote($objConn1, getGet("tbl"), getGet("idCol"));
$myQuote = "";
$strSQL = "select " . getGet("col") . " from " . getGet("tbl") . " where ";
$strSQL .= getGet("idCol") . " = " . $myQuote . getGet("id") . $myQuote;

//fwrite($handle, $strSQL . "\n");
$objRSImage = $objConn1->Execute($strSQL);

if ($objRSImage):
//fwrite($handle, "Recordset OK" . "\n");
$image = $objRSImage->fields[getGet("col")];
$objRSImage->Close();
switch (getGet("type")):
case "jpg":
//fwrite($handle, "Type = jpeg\n");
header("Content-type: image/jpeg");
$myType = "jpg";
break;
case "gif":
//fwrite($handle, "Type = gif\n");
header("Content-type: image/gif");
$myType = "gif";
break;
case "png":
//fwrite($handle, "Type = png\n");
header("Content-type: image/png");
$myType = "png";
break;
default:
//fwrite($handle, "Type = default\n");
if(ord($image[0]) == 255 && ord($image[1]) == 216 &&
ord($image[2]) == 255 && ord($image[3]) == 224):
header("Content-type: image/jpeg");
$myType = "jpg";
endif;
if(strcmp(substr($image,0,3),"GIF") == 0):
header("Content-type: image/gif");
$myType = "gif";
endif;
if(ord($image[0]) == 137 && ord($image[1]) == 80 &&
ord($image[2]) == 78 && ord($image[3]) == 71 &&
ord($image[4]) == 13 && ord($image[5]) == 10 &&
ord($image[6]) == 26 && ord($image[7]) == 10):
header("Content-type: image/png");
$myType = "png";
endif;
endswitch;
header("Content-Disposition: attachment; filename=\"" . getGet("col") .
".$myType" . "\"\n");
header("Content-Transfer-Encoding: binary\n");
header("Content-length: " . strlen($image) . "\n");
//fwrite($handle, "filename=\"" . getGet("col") . ".$myType" . "\"\n");
print($image);
unset($image);
endif;
unset($objRSImage);
$objConn1->Close();
unset($objConn1);
fclose($handle);
?>
The image is not displayed in the html document. Is someone can tell me why
it is not?

Many thanks!

Pierre Tremblay
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Pierre Tremblay wrote:
The image is not displayed in the html document. Is someone can tell
me why it is not?


Loose the Content-Disposition and Content-Transfer-Encoding headers. The
Content-Length can also be left out in most cases.

BTW, when using GET or POST parameters to construct a query, it's always
advisable to validate them first, but I assume that the getGet() function
takes care of that?
JW

Jul 17 '05 #2

P: n/a
Hi!

Thanks for your reply!

I commented the corresponding header call as per your suggestion but
unfortunately, I still have no image in my html document.

The sql is ok (I even at some point used fwrite to write the content of the
recordset field in a file to verify that I retrieved something).

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41***********************@news.euronet.nl...
Pierre Tremblay wrote:
The image is not displayed in the html document. Is someone can tell
me why it is not?


Loose the Content-Disposition and Content-Transfer-Encoding headers. The
Content-Length can also be left out in most cases.

BTW, when using GET or POST parameters to construct a query, it's always
advisable to validate them first, but I assume that the getGet() function
takes care of that?
JW

Jul 17 '05 #3

P: n/a

[ TOFU corrected: <http://tk.digiserv.net/tofu.txt> ]

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41***********************@news.euronet.nl. ..
BTW, when using GET or POST parameters to construct a query, it's always
advisable to validate them first, but I assume that the getGet() function
takes care of that?


On Thu, 9 Sep 2004 19:42:41 -0400, "Pierre Tremblay"
<pi*****@pierretci.com> wrote:
Hi!

Thanks for your reply!

[ snip ]

The sql is ok (I even at some point used fwrite to write the content of the
recordset field in a file to verify that I retrieved something).

I think you're missing JW's point =)

It's not whether it retrieved anything, it's _validation_.

What happens when someone terminates the query early and appends
something like exec('rm -rf /'); to the query? Depending on the system
configuration, this could cause some headaches for some people =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #4

P: n/a
Hi!

Not sure if I follow you on this.

This is executed on my test machine (my workstation) and nothing is
terminating the query early. I know that field is a jpeg image and that
field is of type image (in the MSSQL design table screen).

Any help at this point will be greatly appreciated!

Many thanks!
The sql is ok (I even at some point used fwrite to write the content of therecordset field in a file to verify that I retrieved something).

I think you're missing JW's point =)

It's not whether it retrieved anything, it's _validation_.

What happens when someone terminates the query early and appends
something like exec('rm -rf /'); to the query? Depending on the system
configuration, this could cause some headaches for some people =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #5

P: n/a

[ TOFU corrected for the _last_ time, meet killfile next time ]

>The sql is ok (I even at some point used fwrite to write the content ofthe >recordset field in a file to verify that I retrieved something).

I think you're missing JW's point =)

It's not whether it retrieved anything, it's _validation_.

What happens when someone terminates the query early and appends
something like exec('rm -rf /'); to the query? Depending on the system
configuration, this could cause some headaches for some people =)


On Thu, 9 Sep 2004 21:08:48 -0400, "Pierre Tremblay"
<pi*****@pierretci.com> wrote: Hi!

Not sure if I follow you on this.

This is executed on my test machine (my workstation) and nothing is
terminating the query early. I know that field is a jpeg image and that
field is of type image (in the MSSQL design table screen).

Any help at this point will be greatly appreciated!

Many thanks!

OK, I hadn't read your OP. I jumped on JW's bandwagon without realising
you're _reading_ from not writing _to_ the DB =)

From your OP code:
header("Content-Disposition: attachment; "filename=\"" . getGet("col") . ".$myType" . "\"\n");
header("Content-Transfer-Encoding: binary\n");
header("Content-length: " . strlen($image) . "\n");

I see no Content-type header here. I don't use the above 3 headers for
displaying images, but you might find replacing those with:
header("Content-type: image/$myType");
will display your image (assuming the DB retrieval is all correct and
present).
HTH.

Regards,

Ian
PS: Please learn to post correctly, top-posting is _not_ the correct way
to reply to a post.

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #6

P: n/a
Ian.H wrote:
OK, I hadn't read your OP. I jumped on JW's bandwagon without
realising you're _reading_ from not writing _to_ the DB =)


When the user is permitted to both read and write, the following could
happen:

$sql = "select * from " . $_GET['table'];

script.php?table=foo%3Bdelete+from+foo

(this is a classic)
JW

Jul 17 '05 #7

P: n/a

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:6o********************************@4ax.com...

OK, I hadn't read your OP. I jumped on JW's bandwagon without realising
you're _reading_ from not writing _to_ the DB =)

From your OP code:
header("Content-Disposition: attachment; "filename=\"" . getGet("col") . ".$myType" . "\"\n"); header("Content-Transfer-Encoding: binary\n");
header("Content-length: " . strlen($image) . "\n");

I see no Content-type header here. I don't use the above 3 headers for
displaying images, but you might find replacing those with:


The Content-type is set in the switch statement in the code posted in the
original message.

here is the code snippet

switch (getGet("type")):
case "jpg":
fwrite($handle, "Type = jpeg\n");
header("Content-type: image/jpeg"); <--------------- Here
$myType = "jpg";
break;
case "gif":
//fwrite($handle, "Type = gif\n");
header("Content-type: image/gif");
$myType = "gif";
break;
case "png":
//fwrite($handle, "Type = png\n");
header("Content-type: image/png");
$myType = "png";
break;
default:
//fwrite($handle, "Type = default\n");
if(ord($image[0]) == 255 && ord($image[1]) == 216 &&
ord($image[2]) == 255 && ord($image[3]) == 224):
header("Content-type: image/jpeg");
$myType = "jpg";
endif;
if(strcmp(substr($image,0,3),"GIF") == 0):
header("Content-type: image/gif");
$myType = "gif";
endif;
if(ord($image[0]) == 137 && ord($image[1]) == 80 &&
ord($image[2]) == 78 && ord($image[3]) == 71 &&
ord($image[4]) == 13 && ord($image[5]) == 10 &&
ord($image[6]) == 26 && ord($image[7]) == 10):
header("Content-type: image/png");
$myType = "png";
endif;
endswitch;
header("Content-Disposition: attachment; filename=\"" . "Photo" .
".$myType" . "\"\n");
header("Content-Transfer-Encoding: binary\n");
header("Content-length: " . strlen($image) . "\n");
//fwrite($handle, "filename=\"" . getGet("col") . ".$myType" . "\"\n");
print($image);:

Jul 17 '05 #8

P: n/a
On Fri, 10 Sep 2004 08:27:27 -0400, "Pierre Tremblay"
<pi*****@pierretci.com> wrote:
From your OP code:
> header("Content-Disposition: attachment; "filename=\"" .getGet("col") . ".$myType" . "\"\n"); > header("Content-Transfer-Encoding: binary\n");
> header("Content-length: " . strlen($image) . "\n");

I see no Content-type header here. I don't use the above 3 headers for
displaying images, but you might find replacing those with:


The Content-type is set in the switch statement in the code posted in the
original message.

here is the code snippet

switch (getGet("type")):
case "jpg":
fwrite($handle, "Type = jpeg\n");
header("Content-type: image/jpeg"); <--------------- Here
$myType = "jpg";
break;

Ah crap, sorry about that, completely missed it =)

Appears a strange issue. Have you tried forcing a text/plain header to
see what the actual contents of your image are? or try writing the image
data out as an actual image and then trying to view it normally?

Just a couple of other things I might try if I had the same issue.

Regards,

tk

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #9

P: n/a

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:c4********************************@4ax.com...
On Fri, 10 Sep 2004 08:27:27 -0400, "Pierre Tremblay"
<pi*****@pierretci.com> wrote:
Ah crap, sorry about that, completely missed it =)

Appears a strange issue. Have you tried forcing a text/plain header to
see what the actual contents of your image are? or try writing the image
data out as an actual image and then trying to view it normally?

Just a couple of other things I might try if I had the same issue.


I found the problem!

The fwrite statement gave me a clue when I added a line to write the len of
the data retrieved. No matter what row I was reading, all image field was
4096 bytes in length.

So a simple trip to php.ini and setting the text_limit and text_size entries
to a bigger size fixed the problem (default was 4096).

Many thanks again for your help!
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.