Re: set filename of file open/download
| From: | Richard Lynch | Date: | Sat, 24 Aug 2002 23:51:51 +0000 |
| Subject: | Re: set filename of file open/download | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-114004@lists.php.net to get a copy of this message | ||
>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.
--
Like Music? http://l-i-e.com/artists.htm