Solved

Cross Browser CSV Download

Posted on 2011-09-21
7
780 Views
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 mysite.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.


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.
0
Comment
Question by:OmniUnlimited
7 Comments
 
LVL 3

Expert Comment

by:woepwobin
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?
0
 
LVL 27

Expert Comment

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

Assisted Solution

by:MattJellings
MattJellings earned 450 total points
ID: 36579205
Hi,

Take a look at this page: http://stackoverflow.com/questions/217424/create-a-csv-file-for-a-user-in-php/6493794#6493794

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.

Cheers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
MattJellings earned 450 total points
ID: 36582397
Hi,

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;
	
	exit();

Open in new window


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

Hope that helps,

Cheers

Matt
0
 
LVL 3

Assisted Solution

by:woepwobin
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.
0
 
LVL 17

Author Closing Comment

by:OmniUnlimited
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.
0
 
LVL 4

Expert Comment

by:MattJellings
ID: 36586171
Hi,

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

Cheers

Matt
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

939 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now