FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Pls help with output of csv file to browser?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Pls help with output of csv file to browser? [message #179289 is a reply to message #179280] Tue, 02 October 2012 03:41 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Sun, 30 Sep 2012 11:19:09 -0400, BobMCT wrote:

> I'm trying to add the generation of a csv file

When I want to output dynamically generated csv data to a user (such as
the raw output from a mysql db query to use in a spreadsheet), I include
a link to a php file that generates the csv file eg:

<a href="query6csv.php">get csv data here</a>

This dynamically generates csv data and sends it with the appropriate
mime type (ie text/csv) using the php header function, it also sends a
content disposition header with a suggested filename to save the file as.

<?php // start of file - must be at very start of line 1!

header( "Content-Type: text/csv" );

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

/*

generate and send send csv data here

*/

// end of file - do not use question mark + right angle bracket!

If your csv file is pre generated, just link to the csv file <a
href="somefile.csv">get csv data here</a>, and make sure your server
serves files of ".csv" as mime type "text/csv".

Note that it's important to have no white space at all preceding the
opening "<php" in the dynamically generated csv case, as the presence of
white space before the php code will trigger standard headers to be sent.

Likewise, don't terminate the php code with "?>" as if you do, anything
after that termination will get tacked onto the end of your csv data
file, possibly rendering it useless to the recipient uinless they
manually clean it up first.

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: FEDEX Shipping
Next Topic: Asynchronous execution of PHP file
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 04:08:26 GMT 2024

Total time taken to generate the page: 0.04784 seconds