Pls help with output of csv file to browser?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm trying to add the generation of a csv file to send to the
requestor's browser.  So far I do have it working (sort of) by sending
special headers indicating a text file and using fputcsv.  The user
receives the expected what to do prompt and if saved the dialog box
asking where to save it appears and works.

That's the good part.

The bad part is that there is no resulting/completion page.  And, if I
examine the saved csv file it actually contains the html for the
subsequent page and the originating page still resides on the
requestor's screen.

My guess would be that once the file is sent another set of header
details must be sent to tell the browser that html is now coming.
But, I cannot locate any examples of what to send in this case.

I'm hoping that some of you readers have done this before and can
share the technique/mechanism used to accomplish this successfully.

Any ideas/examples/suggestions greatly appreciated.


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

On Sun, 30 Sep 2012 11:19:09 -0400, BobMCT wrote:
Quoted text here. Click to load it

I've not done this, but if you send the starting page, have that open up
a new tab-page-whatever that handles the download, and then the display
page uses javascript to query the server occasionally (or listens for a
semaphore) to find out when the CSV-sender program has finished, then
the displayed page can trigger the refresh to the completion page.

Remember, a 12'x12'x18" raised floor can hold over a thousand gallons of
blood before it starts to seep up through the cracks.
              -- Roger Burton West in the Monastery

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

BobMCT wrote:

Quoted text here. Click to load it

There is not supposed to be one.  The moment the download starts, control is
turned over to the client which hopefully (and usually) provides the feature
to show the progress of the download.

Quoted text here. Click to load it

That is BAD.  Borken As Designed.

Quoted text here. Click to load it

It should be possible to send an HTTP multi-part response message as defined
in < .  
However, since that might not be supported, you should include a link for
downloading *only* the CSV in the HTML part anyway.

That is, if you absolutely must display an HTML document along with the CSV
download.  The simpler way is not so send any HTTP response containing
*HTML* on success at all, for example:

  /* process data */
  if ($success)
    header('Content-Type: text/csv; charset=UTF-8');  
    header('Content-Disposition: attachment; filename=my.csv');  
    fputcsv('php://stdout', …);
    header('Content-Type: text/html; charset=UTF-8');
    require_once 'error.php';

You may also use, but should never rely on, client-side processing.

When all you know is jQuery, every problem looks $(olvable).

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

On 9/30/2012 11:19 AM, BobMCT wrote:
Quoted text here. Click to load it
What I do (which is not a recommendation ;-) is:
using javascript I have the originating page (the page before the
1. the button (or whatever) opens a new page, using'new_script.php?args_as_necessary')
2. then submits

the invokes new_script.php that actually generates
the csv and send it with the appropriate headers.  Firefox will
put the csv in a new tab on top.

the submit of the originating page generates and sends the
subsequent page (which probably will be under the csv)

the user saves/prints the csv and closes that tab and the
subsequent page pops up.

Yeah it requires javascript, not a problem in my setting (intranet).

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

bill wrote:
Quoted text here. Click to load it

In my main page ( there is a button to
download a CSV file

Th button is called in te page usiing this

web_button(200,20,"Download", "Download Data","/downloads/grid.csv");
function that does the  button presentation is this

function web_button($x,$y,$label, $alt,$url)
<div class="button" onmouseover="this.className='hot-button'"
style="Position: absolute; top:<?echo $y;?>px; left:<?echo $x;?>px;
height: 35px; width: 150px; text-align:center;">
<a class="mybutton" href="<?echo $url?>"><? echo $label?></a>
You can grab the CSS file probably as well from this site if you like
the button.

Now this csv file is built every hour by cron, from the database. If you
wanted to instead have a button that constructs the data on an as
requested basis, the "/downloads/grid.csv" needs replacing with a
pointer to your script.

That script should send headers consistent with the data being a CSV,
and then the data.

And nothing else.

in general the browser will respond to data which is not displayable -
i.e. not an image or  HTML - as defined by the headers, by either
inviting you to download it and save it, or invoking some application
that it thinks will recognise it, or by passing it to a plugin to handle it.

I.e. it will not necessarily *follow* (in the sense of replacing the
current web page)  a hyperlink to downloadable data

I hope this helps. If you have more queries ask here.


(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.

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

On 9/30/2012 11:19 AM, BobMCT wrote:
Quoted text here. Click to load it

Not possible the way you're trying to do it.  A request to the server
can return exactly one content-type.  It can be an image, a web page
(i.e. html), a document (i.e. .csv) or whatever.  You are trying to
return two different types (a .csv followed by a .html) in the same
request.  It's not allowed.

You also can't send javascript or an html redirect in a .csv file - the
browser will just consider that part of the .csv file.  And you can't
send an http redirect followed by the .csv file.

IOW, your design won't work.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

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

On Sun, 30 Sep 2012 11:19:09 -0400, BobMCT wrote:

Quoted text here. Click to load it

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.


Denis McMahon

Site Timeline