Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7219

Re: Pls help with output of csv file to browser?

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: Pls help with output of csv file to browser?
Date 2012-10-02 03:41 +0000
Organization A noiseless patient Spider
Message-ID <k4dnpp$3bb$2@dont-email.me> (permalink)
References <h7og68tjuulikujgrv1k7usbtue38a4s69@4ax.com>

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Pls help with output of csv file to browser? BobMCT <r.mariotti@fdcx.net> - 2012-09-30 11:19 -0400
  Re: Pls help with output of csv file to browser? "Peter H. Coffin" <hellsop@ninehells.com> - 2012-09-30 10:41 -0500
  Re: Pls help with output of csv file to browser? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 19:01 +0200
  Re: Pls help with output of csv file to browser? bill <william@TechServSys.com> - 2012-10-01 08:16 -0400
    Re: Pls help with output of csv file to browser? The Natural Philosopher <tnp@invalid.invalid> - 2012-10-01 23:28 +0100
  Re: Pls help with output of csv file to browser? Jerry Stuckle <jstucklex@attglobal.net> - 2012-10-01 14:50 -0400
  Re: Pls help with output of csv file to browser? Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-02 03:41 +0000

csiph-web