473,562 Members | 3,023 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 35599
"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 #80619929990054 1, 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.or g/> <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$x f.67631@lakerea d04> 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.p ng', 'rb');
if ($tag) {
header('Content-Type: image/png');
fpassthru($tag) ;
}
Jul 16 '05 #4
Gerhard Fiedler <no****@globo.c om.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 #80619929990054 1, 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.or g/> <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*******@REMO VE.MEearthlink. INVALID> wrote in message
news:pa******** *************** *******@REMOVE. MEearthlink.INV ALID...
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:
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.p ng', '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.c om.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$x f.46220@lakerea d04> 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_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.

Jul 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2947
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 might, I can't get binary to work (Word b0rks trying to open the resulting file). When I create a simple RTF in Wordpad and then view its source,...
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
37078
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
2260
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
3466
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
1886
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 consists of 9 variables of Integer: Signed 32-bit integer (4 bytes) and 8 variables of Double: Signed 64-bit IEEE double-precision floating point...
2
2068
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 of the file of the binary file..... Any suggestions
0
7652
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7867
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7625
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7934
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6219
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3621
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2073
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1187
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
901
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.