Getting an image from a PHP source and ADOdb

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pierre Tremblay

    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.ph p?tbl=dbo.Emplo yees&type=jpg&c ol=Photo&idCol= EmployeeID&id=1
    Border=0 alt=''>&nbsp;</td>


    This call getimage.php source. Following is the php source itself:

    <?PHP
    include_once('o rdersappdata.ph p');


    $objConn1 = &ADONewConnecti on("mssql");
    $objConn1->debug = $DebugMode;
    $objConn1->Connect("serve r1","sa","sa200 0","northwind") ;
    include_once('u tils.php');
    $myQuote = "";
    $objRSImage = "";
    $strSQL = "";
    $image = "";
    $myType = "";

    $handle = "";
    $filename = 'test.txt';
    $handle = fopen($filename , 'a');
    //fwrite($handle, "Debug open");
    //$myQuote = getQuote($objCo nn1, 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($strSQ L);

    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(subst r($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($objRSIma ge);
    $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


  • Janwillem Borleffs

    #2
    Re: Getting an image from a PHP source and ADOdb

    Pierre Tremblay wrote:[color=blue]
    > The image is not displayed in the html document. Is someone can tell
    > me why it is not?
    >[/color]

    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



    Comment

    • Pierre Tremblay

      #3
      Re: Getting an image from a PHP source and ADOdb

      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.c om> wrote in message
      news:4140e51f$0 $41701$1b2cd167 @news.euronet.n l...[color=blue]
      > Pierre Tremblay wrote:[color=green]
      > > The image is not displayed in the html document. Is someone can tell
      > > me why it is not?
      > >[/color]
      >
      > 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
      >
      >
      >[/color]


      Comment

      • Ian.H

        #4
        Re: Getting an image from a PHP source and ADOdb


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

        [color=blue]
        >"Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
        >news:4140e51f$ 0$41701$1b2cd16 7@news.euronet. nl...[/color]
        [color=blue][color=green]
        >> 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?[/color][/color]

        On Thu, 9 Sep 2004 19:42:41 -0400, "Pierre Tremblay"
        <pierret@pierre tci.com> wrote:
        [color=blue]
        >Hi!
        >
        >Thanks for your reply![/color]


        [ snip ]

        [color=blue]
        >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).[/color]


        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

        Comment

        • Pierre Tremblay

          #5
          Re: Getting an image from a PHP source and ADOdb

          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!
          [color=blue][color=green]
          > >The sql is ok (I even at some point used fwrite to write the content of[/color][/color]
          the[color=blue][color=green]
          > >recordset field in a file to verify that I retrieved something).[/color]
          >
          >
          > 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/[/color]


          Comment

          • Ian.H

            #6
            Re: Getting an image from a PHP source and ADOdb


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

            [color=blue][color=green][color=darkred]
            >> >The sql is ok (I even at some point used fwrite to write the content of[/color][/color]
            >the[color=green][color=darkred]
            >> >recordset field in a file to verify that I retrieved something).[/color]
            >>
            >>
            >> 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 =)[/color][/color]

            [color=blue]
            > On Thu, 9 Sep 2004 21:08:48 -0400, "Pierre Tremblay"
            > <pierret@pierre tci.com> wrote:[/color]
            [color=blue]
            > 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![/color]


            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:
            [color=blue]
            > header("Content-Disposition: attachment; "filename=\ "" . getGet("col") . ".$myType" . "\"\n");
            > header("Content-Transfer-Encoding: binary\n");
            > header("Content-length: " . strlen($image) . "\n");[/color]


            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

            Comment

            • Janwillem Borleffs

              #7
              Re: Getting an image from a PHP source and ADOdb

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

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

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

              script.php?tabl e=foo%3Bdelete+ from+foo

              (this is a classic)


              JW



              Comment

              • Pierre Tremblay

                #8
                Re: Getting an image from a PHP source and ADOdb


                "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
                news:6o22k0lqk2 jvv4v6gvqna0tfl 3val4kq8a@4ax.c om...[color=blue]
                >
                > 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:
                >[color=green]
                > > header("Content-Disposition: attachment; "filename=\ "" .[/color][/color]
                getGet("col") . ".$myType" . "\"\n");[color=blue][color=green]
                > > header("Content-Transfer-Encoding: binary\n");
                > > header("Content-length: " . strlen($image) . "\n");[/color]
                >
                >
                > 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:
                >[/color]

                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(subst r($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);:



                Comment

                • Ian.H

                  #9
                  Re: Getting an image from a PHP source and ADOdb

                  On Fri, 10 Sep 2004 08:27:27 -0400, "Pierre Tremblay"
                  <pierret@pierre tci.com> wrote:
                  [color=blue][color=green]
                  >> From your OP code:
                  >>[color=darkred]
                  >> > header("Content-Disposition: attachment; "filename=\ "" .[/color][/color]
                  >getGet("col" ) . ".$myType" . "\"\n");[color=green][color=darkred]
                  >> > header("Content-Transfer-Encoding: binary\n");
                  >> > header("Content-length: " . strlen($image) . "\n");[/color]
                  >>
                  >>
                  >> 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:
                  >>[/color]
                  >
                  >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;[/color]


                  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

                  Comment

                  • Pierre Tremblay

                    #10
                    Re: Getting an image from a PHP source and ADOdb


                    "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
                    news:c4b3k056eg 4nk7l5fgjgmm0s2 1p4f7uqtk@4ax.c om...[color=blue]
                    > On Fri, 10 Sep 2004 08:27:27 -0400, "Pierre Tremblay"
                    > <pierret@pierre tci.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.[/color]

                    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!


                    Comment

                    Working...