fopen to read binary file (jpg, pdf, etc.)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    fopen to read binary file (jpg, pdf, etc.)

    I have a PHP script that would read in a binary file and display it as if it
    were <img src>, how would you do that w/o changing the header's MIME type?
    The entire file does not need to be changed.

    Thanks
    Phil


  • MegaZone

    #2
    Re: fopen to read binary file (jpg, pdf, etc.)

    "Phil Powell" <soazine@erols. com> shaped the electrons to say:[color=blue]
    >I have a PHP script that would read in a binary file and display it as if it
    >were <img src>, how would you do that w/o changing the header's MIME type?[/color]

    If you only return the binary content then you must change the MIME
    type of any correctly implemented browser will probably have trouble.
    User agents are supposed to obey the MIME type in the header, so if
    the MIME type is text/html the browser will just so the binary content
    as text. (IE may show the image since it is notorious for ignoring
    MIME types - which is very frustrating when you want it to do the
    right thing.)

    -MZ, RHCE #80619929990054 1, ex-CISSP #3762
    --
    <URL:mailto:meg azone@megazone. org> Gweep, Discordian, Author, Engineer, me..
    "A little nonsense now and then, is relished by the wisest men" 508-755-4098
    <URL:http://www.megazone.or g/> <URL:http://www.eyrie-productions.com/> Eris

    Comment

    • Gerhard Fiedler

      #3
      Re: fopen to read binary file (jpg, pdf, etc.)

      On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:
      [color=blue]
      >(IE may show the image since it is notorious for ignoring
      >MIME types - which is very frustrating when you want it to do the
      >right thing.)[/color]

      Could you please elaborate a bit when that becomes a problem?

      Comment

      • Gary Petersen

        #4
        Re: fopen to read binary file (jpg, pdf, etc.)

        A horsie named Phil Powell demonstrated surprising intellligence and its
        ability to use morse code on Sat, 06 Sep 2003 19:34:21 -0500 when it
        tapped <O2v6b.126972$x f.67631@lakerea d04> with its hoof:
        [color=blue]
        > I have a PHP script that would read in a binary file and display it as
        > if it were <img src>, how would you do that w/o changing the header's
        > MIME type? The entire file does not need to be changed.
        >
        > Thanks
        > Phil[/color]

        $tag = fopen('myfile.p ng', 'rb');
        if ($tag) {
        header('Content-Type: image/png');
        fpassthru($tag) ;
        }

        Comment

        • MegaZone

          #5
          Re: fopen to read binary file (jpg, pdf, etc.)

          Gerhard Fiedler <nospam@globo.c om.REMOVE> shaped the electrons to say:[color=blue]
          >On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:[color=green]
          >>(IE may show the image since it is notorious for ignoring
          >>MIME types - which is very frustrating when you want it to do the
          >>right thing.)[/color]
          >Could you please elaborate a bit when that becomes a problem?[/color]

          It has been an issue using Perl .cgi to 'download' different binary
          files. IE wouldn't obey the MIME type, but would look at '.cgi' and
          display the content.

          It happens with other extensions, especially if you have something
          with .html which is really a dynamic script that returns different
          content. That *should* work as long as the MIME type is correct, but
          IE seems to give more wait to the file extension. Browsers should
          never use the extension, they should always use the MIME type. If the
          MIME type were missing, then I could see trying to use the extension.
          Or doing magic number detection on the file itself.

          -MZ, RHCE #80619929990054 1, ex-CISSP #3762
          --
          <URL:mailto:meg azone@megazone. org> Gweep, Discordian, Author, Engineer, me..
          "A little nonsense now and then, is relished by the wisest men" 508-755-4098
          <URL:http://www.megazone.or g/> <URL:http://www.eyrie-productions.com/> Eris

          Comment

          • Phil Powell

            #6
            Re: fopen to read binary file (jpg, pdf, etc.)

            Thanx, but I can't use header() because the image is embedded inside
            text/html content type.

            Phil

            "Gary Petersen" <garyp1492@REMO VE.MEearthlink. INVALID> wrote in message
            news:pan.2003.0 9.07.19.24.34.9 759.334@REMOVE. MEearthlink.INV ALID...[color=blue]
            > A horsie named Phil Powell demonstrated surprising intellligence and its
            > ability to use morse code on Sat, 06 Sep 2003 19:34:21 -0500 when it
            > tapped <O2v6b.126972$x f.67631@lakerea d04> with its hoof:
            >[color=green]
            > > I have a PHP script that would read in a binary file and display it as
            > > if it were <img src>, how would you do that w/o changing the header's
            > > MIME type? The entire file does not need to be changed.
            > >
            > > Thanks
            > > Phil[/color]
            >
            > $tag = fopen('myfile.p ng', 'rb');
            > if ($tag) {
            > header('Content-Type: image/png');
            > fpassthru($tag) ;
            > }[/color]


            Comment

            • Gerhard Fiedler

              #7
              Re: fopen to read binary file (jpg, pdf, etc.)

              On 7 Sep 2003 23:17:50 GMT, MegaZone wrote:
              [color=blue]
              >Gerhard Fiedler <nospam@globo.c om.REMOVE> shaped the electrons to say:[color=green]
              >>On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:[color=darkred]
              >>>(IE may show the image since it is notorious for ignoring
              >>>MIME types - which is very frustrating when you want it to do the
              >>>right thing.)[/color]
              >>Could you please elaborate a bit when that becomes a problem?[/color]
              >
              >It has been an issue using Perl .cgi to 'download' different binary
              >files. IE wouldn't obey the MIME type, but would look at '.cgi' and
              >display the content.[/color]

              Thanks, makes sense (or not... :). I guess too many Windows developers
              in the IE team :)

              Comment

              • Gary Petersen

                #8
                Re: fopen to read binary file (jpg, pdf, etc.)

                A horsie named Phil Powell demonstrated surprising intellligence and
                its ability to use morse code on Sun, 07 Sep 2003 20:41:53 -0500 when
                it tapped <98R6b.135680$x f.46220@lakerea d04> with its hoof:
                [color=blue]
                > Thanx, but I can't use header() because the image is embedded inside
                > text/html content type.
                >[/color]

                The text/html content only links to images. It does not
                contain them as such. So you would have a PHP page
                that creates the text/html content, and that content
                would contain IMG elements that link to another PHP
                script that generates images only. That script is
                free to use header() to specify an image type.

                In the following example I got lazy and decided
                to put everything into one file:

                <?php
                // PHP 4.0.5
                error_reporting (E_ALL & ~E_NOTICE);

                $s = & $HTTP_SERVER_VA RS;
                if (empty($HTTP_GE T_VARS['img'])):
                ?>
                <title> Image Show </title>
                <p> This page should show an image.
                Here it is:
                </p>
                <img src='<?php echo "$s[PHP_SELF]?img=myimage%2E png"; ?>'
                alt='A nice image'>
                <?php
                else:
                $imgfile = $HTTP_GET_VARS['img'];
                $tag = fopen($imgfile, 'rb')
                or die('Can\'t read image file');
                if ($tag) {
                header('Content-type: image/png');
                fpassthru($tag) ;
                }
                endif;
                ?>

                I'll leave separating this script into its various
                parts as an exercise for you :-)

                However, you will probably notice that separation is
                not strictly necessary.

                This message is under the GPL.

                Comment

                Working...