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 #170012 is a reply to message #170003] Tue, 05 October 2010 15:44 Go to previous messageGo to previous message
PlastiqueMAN is currently offline  PlastiqueMAN
Messages: 5
Registered: September 2010
Karma:
Junior Member
On 2010-10-05 16:19:04 +0200, Derek Turner said:

> I want to use MySQL to get a list of email addresses and names and then
> output a .csv file to the browser so that it can be saved and then
> imported into Thunderbird etc.
>
> Having googled around I think the solution will be:
>
> 1. Create and open a local file for writing

You should avoid using temp files for this. Unless the file never
changes, then just upload the file and use readfile() or what ever.

>
> 2. Write the data to the local file and close it
>
> 3. Use appropriate header files and then readfile()
>
> 4. Delete the local file
>
> 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?
>
> 2. Can the headers appear appear anywhere in the script or must they be
> be the first lines?
>
> Other questions depend on your answers to those two, so I'll keep them to
> myself for the moment.
>
> <off topic>
>
> I've exported from TB to see what its .csv output file looks like.
>
> 1. Do I really need to include all those fields?

If TB sais so, then you probably need to include it. I'd check out the
vcard rfc if I where you.
http://www.ietf.org/rfc/rfc2426.txt

>
> 2. Will, for example, OE import them properly?

probably not...

>
> </OT>
>
> tia
>
> Derek


I'd do something like:

// not tested

$query = "SELECT whatever FROM table WHERE whatever = something;";

$pdo = new PDO( /* connection stuff*/ );
$stm = $pdo->query($query, PDO::FETCH_ASSOC);

header('Content-Type: text/csv; charset=UTF-8'); // replace UTF-8 with
whatever charset you use
header('Content-Disposition: attachment; filename=mycsvfile.csv');

foreach ($stm->fetchAll() as $csv_line)
{
echo implode(',' $csv_line) . "\r\n";
}
[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 11:44:07 GMT 2024

Total time taken to generate the page: 0.04961 seconds