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

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

P: n/a
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
Jul 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Phil Powell" <so*****@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:me******@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
Jul 16 '05 #2

P: n/a
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?

Jul 16 '05 #3

P: n/a
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);
}
Jul 16 '05 #4

P: n/a
Gerhard Fiedler <no****@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:me******@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
Jul 16 '05 #5

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

Phil

"Gary Petersen" <ga*******@REMOVE.MEearthlink.INVALID> wrote in message
news:pa******************************@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);
}

Jul 16 '05 #6

P: n/a
On 7 Sep 2003 23:17:50 GMT, MegaZone wrote:
Gerhard Fiedler <no****@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 :)
Jul 16 '05 #7

P: n/a
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.

Jul 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.