views:

70

answers:

4

I'm using PHP to create a CSV file from a database query. I run the query, set the headers, and load the page up in Firefox and the file promps for download and opens in excel just like it's supposed to. When I try it in IE, I get an error saying Internet Explorer cannot download ReportPrint.php from www.website.com.
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.

Not sure what can be done to fix this.

header('Content-Description: File Transfer');
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename=Report.csv");
header("Pragma: no-cache");
header("Expires: 0");

echo "record1,record2,record3\n";
A: 

Check the security settings of IE, may be it's configured not to download CSV files.

rubayeet
I checked the settings and didn't see anything right off. then I went to another website and was able to download a dynamically created CSV. Lastly I created a static CSV `test.csv` and it downloaded just fine, so I'm fairly sure that it has something to do with the headers.
AndyD273
Also, one of my co-workers tried and also failed to download the generated CSV on a fresh out of the box version of windows 7.
AndyD273
A: 

Change

header("Content-Disposition: attachment; filename=Report.csv");

to

header("Content-Disposition: attachment;filename=Report.csv");
hopeseekr
+1  A: 

Remove the Pragma: no-cache header. This header prevents IE from downloading the file.

jmz
Well, this might have done it... Removing no-cache didn't solve it, but I found another place were I download PDF files from my site and noticed a few lines I didn't have. I think it was `header("Pragma: public")` that solved it.
AndyD273
It appears that removing the header was the actual fix for you. However, it's not enough to stop adding it: PHP can add it by itself if you are using sessions.
Álvaro G. Vicario
+1  A: 

Internet Explorer tends to display these error messages when it's not able to cache the file and you appear to be trying to avoid caching. Try instead something like this:

<?php

$seconds = 30;

header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$seconds) . ' GMT');
header('Cache-Control: max-age=' . $seconds . ', s-maxage=' . $seconds . ', must-revalidate, proxy-revalidate');

session_cache_limiter(FALSE); // Disable session_start() caching headers
if( session_id() ){
    // Remove Pragma: no-cache generated by session_start()
    if( function_exists('header_remove') ){
        header_remove('Pragma');
    }else{
        header('Pragma:');
    }
}

?>

Adjust $seconds to your liking but don't set it to zero.

It also helps to use mod_rewrite to make IE believe that the download is a static file, e.g., http://example.com/Report.csv

Please report back and tell if it works for you.

Álvaro G. Vicario
Once I put `Pragma: Public` it started working. Changing expires didn't seem to make any difference... Dunno
AndyD273
If I'm not wrong, the only valid value for Pragma is `no-cache`. Setting it to anything else just makes the browser ignore it (since it's not a known value) and is the same as removing the header. Anyway, I'm glad it fixed your issue.
Álvaro G. Vicario