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

Home » Imported messages » comp.lang.php » Send .csv file to browser
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Send .csv file to browser [message #170010 is a reply to message #170003] Tue, 05 October 2010 15:20 Go to previous messageGo to previous message
Michael is currently offline  Michael
Messages: 9
Registered: September 2010
Karma:
Junior Member
On 10/5/2010 9:19 AM, Derek Turner wrote:
> Questions:
>
> 1. Is this a reasonable approach? Or should I be outputting to the
> browser on the fly? Or using PEAR or something like that?

Not necessary to use a local file. You can just stream it straight to
the browser with the appropriate headers. Something like this will
cause the browser to prompt for saving the CSV file. Others may have
refining suggestions, but this has worked for me.

session_cache_limiter('public');
header("Content-type: text/csv");
header("Content-description: File Transfer");
header("Content-disposition: attachment; filename=\"SOMEFILENAME.csv\"");

// Maybe some other caching headers...
header("Pragma: public");
header("Cache-control: max-age=0");
header("Expires: 0");

// Then just echo out your data
foreach ($data as $d)
{
echo implode("," $d) . "\n";
}

> 2. Can the headers appear appear anywhere in the script or must they be
> be the first lines?

They don't have to be the first line of the script, but they *do* have
to be the first line of output. That includes making sure there's no
blank space or line break before the first opening <?php

--
Michael
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: file access permission?
Next Topic: Process queue without cron jobs
Goto Forum:
  

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

Current Time: Sat Nov 30 12:15:20 GMT 2024

Total time taken to generate the page: 0.04332 seconds