473,803 Members | 4,139 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 35623
"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
2977
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, Wordpad seems to be embedding the image as hex. Does anyone have a suggestion as to how I could read an...
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
37121
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
2278
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
3498
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
1899
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 number (8 bytes). when i tried to assign integer and double data type for these variables i am...
2
2080
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
9699
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10542
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10289
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5496
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2968
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.