Richard Lynch wrote
On Saturday, August 24, 2002 4:52 PM
To: David Buerer

> >I'm using a php script to stream a microsoft word file to a browser.
> >
> >I'm sending header("Content-type: {$mimetype[DOC]} \n"); to set the mime
> >type to application/msword so that the browser knows how to open the
file.
> >
> >
> >However, some users systems are configured to allow the choice of open or
> >saveas.  The problem is that when the user selects saveas the filename it
> >uses is the name of the php script which streams the file, not the actual
> >filename of the file.  How do I set this filename?

> Alas, different browsers do this differently...

> The *BEST* way to do it is to make your link be named the filename you
want
> them to download:

> Example:
> http://uncommonground.com/events.pdf

> This is *really* a PHP script that happens to have a filename of
> 'events.pdf'

> I've put this into my .htaccess file to force Apache to parse events.pdf
as
> PHP:

> <Files events.pdf>
>  ForceType application/x-httpd-php
> </Files>

> So Apache "knows" it's a PHP file and PHP spews out valid PDF content and
> the browser never even "sees" .php anywhere, so the browser thinks it's
just
> getting a static PDF file.

> If you have a zillion files to link to, you might want to consider:

> Creating "broken" links to http://example.com/index/nonexistent.doc *BUT*
> you have a file named 'index' and a ForceType like above to make Apache
> parse it as PHP, and inside 'index' you do:

> <?php
>  # Use $path_info to determine what file they wish to download.
>  # NOTE: It's going to be $_ENV['path_info'] or $_SERVER['path_info'] or
> something like that now...
>  # Use phpinfo() to figure out what it is...
>?>

> Or, if that sounds too "tricky" just put the filename into the link, even
> though it looks "bogus":

> http://example.com/download.php/whatever.doc?filename=whatever.doc

> Even though your PHP file is really 'download.php', Apache and PHP don't
> care about that extra crap in there '/whatever.doc' and the browsers are
so
> stupid they think you've got an actual file with that name in it.

> You can also dink with headers involving Content-disposition and something
> else I forgot, since IE and Netscape and different versions of each look
at
> different headers to "decide" what to use for "Save As..."  *BUT* there
will
> always be some minor version of IE or Netscape that doesn't follow their
own
> rules, and the headers won't work for it.  Thus I usually go with the
first
> option, and don't even give the browser any way to "know" that it's not a
> plain old static URL.


I have been struggling with this problem for the last several days.  (See my
recent post, "Trouble Downloading Files, 8.24.02".  The problem I had was
mostly with IE.  Here is how I solved it.
I used the code from the following as the basis of my code:
http://www.zend.com/zend/trick/tricks-august-2001.php
To make it work in IE, I had to add two lines to the section on "send
headers" (See commented lines below).
//send headers
                if(($pc > 1) AND isset($mimetype[$p[$pc - 1]]))
                {
                //display file inside browser
                header("Content-type: " . $mimetype[$p[$pc - 1]] . "\n");
        header("Content-disposition: attachment; filename=\"$file\"\n");   // THIS
LINE IS ADDED TO GIVE A CHOICE TO OPEN, OR SAVE AS FILENAME.
                }
                else
                {
                //force download dialog
                header("Content-type: application/octet-stream\n");
                header("Content-disposition: attachment;
filename=\"$file\"\n");
                }
header('Cache-Control: public');                // THIS LINE IS ADDED TO MAKE  MSIE 
WORK
AT ALL
header("Content-transfer-encoding: binary\n");
                header("Content-length: " . filesize($path) . "\n");
                //send file contents
                $fp=fopen($path, "r");
                fpassthru($fp);

The Cache-Control header came from comments by [EMAIL PROTECTED], 04-Apr-2002
12:25, in http://www.php.net/manual/en/function.fpassthru.php
I'm not sure why these additions fixed the problem, but they did.  Maybe
someone out there with a better understanding than I can explain.  Hope this
helps.
Roger




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to