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


Groups > comp.lang.php > #7213

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

Message-ID <3653659.dzkcXP51QQ@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-09-30 19:01 +0200
Subject Re: Pls help with output of csv file to browser?
Newsgroups comp.lang.php
References <h7og68tjuulikujgrv1k7usbtue38a4s69@4ax.com>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


BobMCT wrote:
^^^^^^
Who?

> 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.

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.

> 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.

That is BAD.  Borken As Designed.

> 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.

It should be possible to send an HTTP multi-part response message as defined 
in <http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.2>.  
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:

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

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


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next 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