473,388 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Getting an image from a PHP source and ADOdb

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
9 3324
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
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

[ 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
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

[ 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
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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vipul Pathak | last post by:
Hello Friends ! I have the Following Code, that Executes a Stored Procedure and Attempt to read a Returned Integer Value from the StoredProc. But It gives Error ... ADODB.Command (0x800A0BB9)...
0
by: gm | last post by:
Immediately after generating the Access application from the Source Safe project I get: "-2147467259 Could not use ''; file already in use." If Access database closed and then reopened I get:...
3
by: CD | last post by:
An application is logging faxes sent in SQL2000 image column type. I have found code on the net but what it is doing is prompting to save to local which is fine for single page image. Not good...
24
by: Manuel | last post by:
Is it possible to embed an image, like a company logo in a CDOSYS generated message? If yes, I´ll apreciate some code sample. I´ve been able to format messages in html the way I like, but I...
2
by: Chris3000 | last post by:
All i wanna do is simple, I wanna display an image from an access database. I try alot of things to no result. Like ascii code or nothing but a blank image with a red 'X'. Here is the code : ...
0
by: bananularstyle | last post by:
Hi everyone, This is an issue I have been picking away at for the past two weeks and am running short of ideas. :-\ I am writing an ASP page that allows a user to open/save a tif image stored on...
0
by: Zw0n1m1r | last post by:
Hallo guyz! :-) I've some problems when I try to perform a image upload into a SQL Server 2000 DB Image field ... I've tried both with the ADO.NET method and the ADODB method: ...
0
by: michels287 | last post by:
With a single record in my table, I can use this code: Private Sub Command1_Click() Set cn = New ADODB.Connection cn.Open "Provider=MSDASQL.1;Persist Security Info=False;" & _ "Data...
3
by: fran7 | last post by:
Hi, I have this nice code that returns a random image database record. It works great. What I am trying to do now is to be able to get the "alt" description for the image from another field. If I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.