473,508 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 16 '05 #1
7 35588
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2936
by: DrewM | last post by:
I'm using ASP to generate an RTF (rich text) file, via the FSO. No problems with text, but when it comes to inserting images, the files have to be embedded as either binary or hex. Try as I...
2
268
by: Pete | last post by:
Hi Could someone kindly help with the C# equivilent of the following 4 lines of C code. I'm *really* struggling with this. ( Colours.dat contains 300 RGB values ) COLORREF Colours;
8
37063
by: S Shulman | last post by:
Hi All I need to read a file to an array of bytes (any type of file) I tried using FileStream and BinaryReader but it doesn't seem to work Thank you, Shmuel
1
2250
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
4
3459
by: Matrixinline | last post by:
Hi All Here is my problem I am using a Unicode project and I tried to read the File like sPath = LPCTSTR; FILE* oFp = _tfopen(sPath,L"r"); while(!feof(oFp))
2
1884
by: kowndinya | last post by:
Hello, i want to read values from a binary file in VisualBasic 6.0 Initially i want to read only header of this binary file. I know this structure for this header. It is 100 bytes long and...
2
2061
by: tulasisridhar | last post by:
Hi All, I have binary file created in Unix I now need to read it using VB.net and doo some procesing. There is some conversion between bigendian and little endians. I know the structure...
0
7401
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...
1
7063
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...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.