views:

723

answers:

2

I'm dynamically generating a text file in PHP, so it has a .php extension but a text/plain MIME type. All browsers display the file as nicely preformatted text, except IE8.

Googling tells me that they've added security where if the HTTP header content type doesn't match the expected content type (I think based on the extension and some sniffing) then it forces the file to be downloaded. In my case I have to open it, and also give it permission to open the file I just told it open! That's probably a Win7 annoyance though. Serving a static plain text file works fine, of course.

So can I stop IE8 from downloading the file and get it to view it normally? The code has to run on multiple shared hosting environments, so I think I'm stuck with the .php extension.

+3  A: 

Add this to your HTTP header:

X-Content-Type-Options: nosniff

It's an IE8 feature to opt-out of its MIME-sniffing.

Source

jimyi
Looks promising from the article, but it's not making a difference I'm afraid!
Michael Hinds
Is there a reason you have to serve text/plain? Could you just serve HTML and format the text with `<pre></pre>`?
jimyi
I take it back, 6 hours later the header magic is working. Must have been the usual over-enthusiastic caching!
Michael Hinds
Incredible useful answer, thx!
mark