RE: Save mySQL data into a CSV file.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

You can use a content header to instruct the browser to treat output as an attachment for saving.  Use something like the following:

<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=filename.csv");
?>

(These *must* be the first lines output by your script)


Then in the body of the script, do something like this:

<?php
echo "$var1,$var2,$var3\n";
?>

Note the commas between each value, and also the '\n' which sends a newline to end each row.  If the user is running Windows with Excel installed, they will be given a choice of opening the file in Excel or saving it.


This works for me using Internet Explorer.  Mozilla appends a .txt to the filename (so you get 'filename.csv.txt').  Does anyone else know how to prevent the .txt being added by Mozilla?


Hope this helps,

Colin Williams.




-----Original Message-----
From: Martin E. Koss [mailto:martin@spirewindows.com]
Sent: 16 June 2003 13:53
To: php-db@lists.php.net
Subject: Save mySQL data into a CSV file.


Hi,
I have a table with 1,008 columns in it and at it's peak it has up to 500
rows of data.
I want to enable my client to click on a link and the next thing they are
prompted with is 'Save or Open' (the CSV file).
The data will be compiled into a comma delimited CSV file but rather than be
printed to the screen it would be best if the browser popped up asking where
to save the 'filename.csv' file to. Once saved, the file can be opened in
Excel for analysis.

Where do I start to do this?

Thanks

Martin



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux