3

Greeting Experts,

I have a php script that queries our db and spits out a csv file in a browser. This work greate in FireFox, Safari, and Chrome. It does not work in Internet Explorer, which is the default browser in our building (sigh). I cannot post the url as it is a protected site and wouldn't do you any good.

In IE I get two popup windows when I click the linke to this report:

1) Unable to download downloads.html from . Unable to open this Internet Site. The request site is either unavailable or cannot be found Please try again later.

2) a downloads % info window: Getting File Information: downloads.html from Estimated time left: Download to: Transfer rate: all these are empty.

So, I have an page, downloads.html trying to produce a CSV file. Not sure what is bombing here as far as IE goes. Are there different headers I can try? I haven't seen any in my searching.

Thanks for any tips...

Here is my function that prints out the data:

# $data is array of data from database
function render_excel($data) {
  # headers for browsers
  header("Content-type: text/csv");
  header("Content-Disposition: attachment; filename=file.csv");
  header("Pragma: no-cache");
  header("Expires: 0");

  # column headers
  $column_headers = "Filename, Link to File, Details, Unique Viewers\n";
  $rows = '';
  foreach($data as $foo => $arr){   
    $rows .= $arr['filename'].",";
    $rows .= $arr['resource_url'].",";
    $rows .= $arr['detail_url'].",";
    $rows .= $arr['distinct_views']."\n";
  }
  print $column_headers;
  print $rows;
}

2 Answers 2

1

There are two problems I think. First is that you need quotes around the filename and second is that you need to add a Content-Length header to show download times.

//1
header("Content-Disposition: attachment; filename=\"file.csv\"");

//2
header("Content-Length: " . (strlen($column_headers.$rows)));

Keep in mind that the length might be different if you have other code on your page.

2
  • Thanks Hugo, but no luck with those changes. I even tried header("Content-type: application/octet-stream"); with no luck
    – John Cowan
    Commented Mar 14, 2013 at 15:47
  • Then I guess Adrian is right and IE is just not expecting dynamic content with an HTML extention. Commented Mar 14, 2013 at 15:59
-1

In my experience, getting this to work with IE - particularly older versions of IE - required using mod_rewrite to redirect a URL with the correct file extension (e.g. download/file.csv) to the PHP file that generates the CSV output. IE for some reason prefers the URL file extension over the content-type and content-disposition headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.