Cross Browser CSV Download

Posted on 2011-09-21
Last Modified: 2012-05-12
Hello Experts!

I need your magnificent help with another problem I am having.  I have set up a site under SSL and am trying to create a dynamic CSV file generated from the contents of the database that can be downloaded when the user clicks on a link.

As of now, I have it set up so that the link redirects to a new PHP page whereupon I am sending the headers and accessing the database to create the CSV file.  This works fine in Firefox, Chrome and Safari.  However in IE I get the following message:

Internet Explorer cannot download download/ from

Internet Explorer was not able to open this Internet site.  The requested site is either unavailable or cannot be found.  Please try again later.

I have tried various methods to get around this situation without success.  I would like to know if any of you experts have code that will work across all browser platforms to accomplish what I am trying to do.
Question by:OmniUnlimited
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 36575884
Always helpful, those messages from IE.

Can you show the code that generates the link, and the code that sends the headers and data?
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36576436
What type of database ? MySQL ?

Assisted Solution

MattJellings earned 450 total points
ID: 36579205

Take a look at this page:

It may be helpful.

I am currently outputting CSV from one of my sites and I've checked and it works in Chrome, FF and IE but unfortunately I don't have the code to hand.  It's likely you may be missing a header or something which is causing IE to complain.  When I get home if you haven't found a solution yet I'll post the code for you.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

MattJellings earned 450 total points
ID: 36582397

Here's the code I'm using, I'll confess I don't fully understand the multiple use of the Content-Type header, but I can say it definitely works in IE 7, 8 and 9.

	header("Pragma: public");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
	header("Content-Type: application/force-download");
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/download");;
	header("Content-Disposition: attachment; filename=file_name_here.csv ");
	header("Content-Transfer-Encoding: binary ");
	echo $data;

Open in new window

The $data variable  is a string variable containing the comma separated data.

Hope that helps,



Assisted Solution

woepwobin earned 50 total points
ID: 36582763
I cannot reproduce the problem.

This is what I usually use:

header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: no-cache, must-revalidate, maxage=1');
header('Cache-Control: maxage=1');
header('Pragma: public');
header('Expires: Fri, 15 Aug 2003 15:00:00 GMT+1'); /* Remember my wedding day :) */
header('Content-Type: application/octet-stream');
header('Content-Length: '.strlen($data));
echo $data;

Open in new window

The only big difference I see with your code is the 'Content-Length' header.
LVL 17

Author Closing Comment

ID: 36584798
MattJellings, thank you for taking time and really analyzing my situation for me.  I as well am puzzled by the need for so many headers, but the important thing is that it works!

Thanks to all the experts who participated.

Expert Comment

ID: 36586171

You're welcome, glad to hear you managed to get it working.



Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Explain Man-in-the-middle attacks on Common Names 3 30
Secure Website 5 39
How do I add date to text file name 15 28
Undefined variable with $_POST in PHP 5 39
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question