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

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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


    Phil Powell Guest

  2. Similar Questions and Discussions

    1. #39767 [NEW]: Binary Read of POST Data
      From: deepak dot vasudevan at lavanyadeepak dot tk Operating system: Windows Server 2003 PHP version: 4.4.4 PHP Bug Type: ...
    2. #25977 [NEW]: fopen('file', 'r'); doesn't move to the end of the file
      From: nightstorm at tlen dot pl Operating system: Windows XP PHP version: 5CVS-2003-10-24 (dev) PHP Bug Type: Filesystem...
    3. [PHP] fopen() || Execute read file as php page; not plain text
      was able to use an absolute reference on my local machine to execute read file contents; e.g. fopen('http://www.mysite.com/index.php', 'a'), but...
    4. read/write binary file
      I'm writing a web service which will return a string containing the contents of a binary file (converted using System.Convert.ToBase64String). When...
    5. Read and write binary data by using ADO
      I use ADO (AppendChunk) in a C++ program to write binary data file into a table column of type image. The image files have almost the size of 1...
  3. #2

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

    "Phil Powell" <soazine@erols.com> shaped the electrons to say:
    >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?
    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 #806199299900541, ex-CISSP #3762
    --
    <URL:mailto:megazone@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.org/> <URL:http://www.eyrie-productions.com/> Eris
    MegaZone Guest

  4. #3

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

    On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:
    >(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.)
    Could you please elaborate a bit when that becomes a problem?

    Gerhard Fiedler Guest

  5. #4

    Default 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$xf.67631@lakeread04> with its hoof:
    > 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
    $tag = fopen('myfile.png', 'rb');
    if ($tag) {
    header('Content-Type: image/png');
    fpassthru($tag);
    }
    Gary Petersen Guest

  6. #5

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

    Gerhard Fiedler <nospam@globo.com.REMOVE> shaped the electrons to say:
    >On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:
    >>(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.)
    >Could you please elaborate a bit when that becomes a problem?
    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 #806199299900541, ex-CISSP #3762
    --
    <URL:mailto:megazone@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.org/> <URL:http://www.eyrie-productions.com/> Eris
    MegaZone Guest

  7. #6

    Default 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@REMOVE.MEearthlink.INVALID> wrote in message
    news:pan.2003.09.07.19.24.34.9759.334@REMOVE.MEear thlink.INVALID...
    > 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$xf.67631@lakeread04> with its hoof:
    >
    > > 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
    >
    > $tag = fopen('myfile.png', 'rb');
    > if ($tag) {
    > header('Content-Type: image/png');
    > fpassthru($tag);
    > }

    Phil Powell Guest

  8. #7

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

    On 7 Sep 2003 23:17:50 GMT, MegaZone wrote:
    >Gerhard Fiedler <nospam@globo.com.REMOVE> shaped the electrons to say:
    >>On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:
    >>>(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.)
    >>Could you please elaborate a bit when that becomes a problem?
    >
    >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.
    Thanks, makes sense (or not... :). I guess too many Windows developers
    in the IE team :)
    Gerhard Fiedler Guest

  9. #8

    Default 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$xf.46220@lakeread04> with its hoof:
    > Thanx, but I can't use header() because the image is embedded inside
    > text/html content type.
    >
    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_VARS;
    if (empty($HTTP_GET_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%2Epng"; ?>'
    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.

    Gary Petersen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139