D. Alvarado wrote:
I have a page that generates a text file (but writes the file with
the ".csv" extension)
Browsers ought to go by the media type of the Content-Type header, if
given, not any filename extensions or URI suffixes.
and then I redirect the user so he can save the file
ITYM, "so he may do with it what he wishes". ;o)
header("Location: $my_csv_file");
However, the file appears as text in the browser
I guess your server isn't configured to send a particular media type
for CSV files and its default is text/plain. Sending CSV files as
text/plain isn't ideal.
and I would prefer that the gray "Download this file" box pop up.
It may not always be possible for a grey "Download this file" box to
pop up; downloading and manually saving the file may not always be
desirable. Short of sending the boys round, you can't force it.
How could I pull this off,
The usual, RTFM-challenged suggestion is to work against the specs and
lie about the media type of the file. You'll find this is touted by
many, including some who've contributed to the Manual's user-
contributed notes, as *the* way.
or is this really more dependent on client browser settings?
Yes.
I appreciate any advice,
Tim's offered the best advice already.
Because there's no registered media type for CSV files, consider
sending a Content-Type header with a media type of text/x-csv. If a
media type is unknown to a browser, it should treat it as
application/octet-stream, which means "arbitrary binary data"
(RFC2046) -- IOW, no other media type was appropriate.
Alternatively, consider sending files in TSV format. There is a
registered media type text/tab-separated-values, and despite its name
the fgetcsv function handles tabs instead of commas just fine.
The protocol correct way of suggesting the presentational style of
data, however, is the Content-Disposition header (RFC2183). I doubt
if it's ever of any use on the web though.
Have a great weekend -- what's left of it!
--
Jock