fopen, fpassthru downloading – PHP

  php

Q(Question):

hello all

im looking to make a download script as i cannot use the .htaccess to
control directory access to files as im using a php login system using
mysql and sessions.

i have been playing about with fopen and fpassthru but having a few
problems making the file download. The files involved would mainly be
but not limited to .pdfs, .docs and .zips

i have created the following:

$filepath = ‘/home/me/folder/file.zip’;
$size = filesize($filepath);

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=download.zip");
header("Content-Length: ".$size);

$ok = fopen($filepath, ‘rb’);
fpassthru($ok);
this pops up the download box and downloads in firefox, but in ie it
pops up the box and then fails. any ideas?

im guessing its something else that i need to put into my headers but
i’ve no idea what. i’ve been playing with cache and the mime type but
this seems to download still in firefox but display the text version of
the zip file in the window in ie.

Thanks for any advice.

A(Answer):

Steve wrote:

hello all

im looking to make a download script as i cannot use the .htaccess to
control directory access to files as im using a php login system using
mysql and sessions.

i have been playing about with fopen and fpassthru but having a few
problems making the file download. The files involved would mainly be
but not limited to .pdfs, .docs and .zips

i have created the following:

$filepath = ‘/home/me/folder/file.zip’;
$size = filesize($filepath);

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=download.zip");
header("Content-Length: ".$size);

$ok = fopen($filepath, ‘rb’);
fpassthru($ok);
this pops up the download box and downloads in firefox, but in ie it
pops up the box and then fails. any ideas?

im guessing its something else that i need to put into my headers but
i’ve no idea what. i’ve been playing with cache and the mime type but
this seems to download still in firefox but display the text version of
the zip file in the window in ie.

Thanks for any advice.

I’ve done with before with almost exactly the same set of headers and
mine works just fine. The only difference being that I use

Content-Type: application/force-download;
instead of your
Content-Type: application/octet-stream;

I know all modern user-agents (including IE) support this type but I
haven’t tested it on old browsers.

A(Answer):

Terence wrote:

I’ve done with before with almost exactly the same set of headers and
mine works just fine. The only difference being that I use

Content-Type: application/force-download;
instead of your
Content-Type: application/octet-stream;

I know all modern user-agents (including IE) support this type but I
haven’t tested it on old browsers.

thanks for your reply, still no luck though

it seems as though every header i try seems to work in firefox but
nothing works in ie, even a combination of headers.

at the moment with:

header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"download.zip\"");
header("Content-Length: ".$size);

it trys to download the actual php file instead, and then fails if i
proceed. this is really confusing because reading around, others have
experienced success with similar headers.

A(Answer):

With total disregard for any kind of safety measures Steve
<ki*******@hotmail.com> leapt forth and uttered:

it seems as though every header i try seems to work in firefox
but nothing works in ie, even a combination of headers.

IE is known for it’s quirky and often deliberatly contrary handling
of MIME types. Most of the times it seems to guess the MIME type from
the originating file header rather than listening to the info that
the server gives it…


Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/

A(Answer):

Phil Roberts wrote:

With total disregard for any kind of safety measures Steve
<ki*******@hotmail.com> leapt forth and uttered:

it seems as though every header i try seems to work in firefox
but nothing works in ie, even a combination of headers.

IE is known for it’s quirky and often deliberatly contrary handling
of MIME types. Most of the times it seems to guess the MIME type from
the originating file header rather than listening to the info that
the server gives it…

it sure does

i’ve found a solution albeit unsatisfactory. the script has to be called
via a link into a new window rather than through the address bar
directly…. if only i knew that 8 hours ago when i was having problems 🙂

A(Answer):

"Steve" <ki*******@hotmail.com> wrote in message
news:pL*********************@stones.force9.net…

i’ve found a solution albeit unsatisfactory. the script has to be called
via a link into a new window rather than through the address bar
directly…. if only i knew that 8 hours ago when i was having problems 🙂

The best solution would be to use Apache rewrite, so that the browser thinks
it’s downloading a file in the normal fashion. Worth looking into.

A(Answer):

"Chung Leong" <ch***********@hotmail.com> wrote in message news:<Sd********************@comcast.com>…

"Steve" <ki*******@hotmail.com> wrote in message
news:pL*********************@stones.force9.net…

i’ve found a solution albeit unsatisfactory. the script has to be called
via a link into a new window rather than through the address bar
directly…. if only i knew that 8 hours ago when i was having problems 🙂

The best solution would be to use Apache rewrite, so that the browser thinks
it’s downloading a file in the normal fashion. Worth looking into.

I’m skeptical, how the mod_rewrite will help here. The OP says
about the problem with downloading zip files via script–which we have
discussed here many times. The download script should be called via
clicking the link; if it is called directly from browser, it will
display the binary output.


http://www.sendmetoindia.com – Send Me to India!
Email: rrjanbiah-at-Y!com

LEAVE A COMMENT