Ask a Question related to PHP Development, Design and Development.
-
Phil Powell #1
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
-
#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: ... -
#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... -
[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... -
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... -
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... -
MegaZone #2
Re: fopen to read binary file (jpg, pdf, etc.)
"Phil Powell" <soazine@erols.com> shaped the electrons to say:
If you only return the binary content then you must change the MIME>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?
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
-
Gerhard Fiedler #3
Re: fopen to read binary file (jpg, pdf, etc.)
On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:
Could you please elaborate a bit when that becomes a problem?>(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.)
Gerhard Fiedler Guest
-
Gary Petersen #4
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:
$tag = fopen('myfile.png', 'rb');> 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
if ($tag) {
header('Content-Type: image/png');
fpassthru($tag);
}
Gary Petersen Guest
-
MegaZone #5
Re: fopen to read binary file (jpg, pdf, etc.)
Gerhard Fiedler <nospam@globo.com.REMOVE> shaped the electrons to say:
It has been an issue using Perl .cgi to 'download' different binary>On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:>Could you please elaborate a bit when that becomes a problem?>>(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.)
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
-
Phil Powell #6
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
-
Gerhard Fiedler #7
Re: fopen to read binary file (jpg, pdf, etc.)
On 7 Sep 2003 23:17:50 GMT, MegaZone wrote:
Thanks, makes sense (or not... :). I guess too many Windows developers>Gerhard Fiedler <nospam@globo.com.REMOVE> shaped the electrons to say:>>>On 7 Sep 2003 07:36:14 GMT, MegaZone wrote:>>Could you please elaborate a bit when that becomes a problem?>>>(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.)
>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.
in the IE team :)
Gerhard Fiedler Guest
-
Gary Petersen #8
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:
The text/html content only links to images. It does not> Thanx, but I can't use header() because the image is embedded inside
> text/html content type.
>
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



Reply With Quote

