Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a CSV 1

Status
Not open for further replies.

cchipman

IS-IT--Management
Sep 16, 2002
125
US
I've got a php page that hooks into the LDAP interface on our Active Directory Servers and that gives out user lists. However, many of our users want to be able to export the list of users to a csv file and save it locally to their harddrive (for importing into older versions of outlook or to a PDA).

Anyone have just the code for writing data to a csv file and allowing them to download it? I would like the data to be generated on-the-fly, so they'll always have the most up-to-date version every time they run it.
 
Creating a CSV is just like writing normal text file with the extension .csv.

The format is comma deliminated.
eg.

$content = "a,b,c,d\r\na1,a2,a3,a4\r\na2";

If you write this, it will come out as:

a b c d
a1 a2 a3 a4
a2

 
<sigh>

I'm really well acquainted with the csv file format. I was more asking

"how do I create a button and/or link so that when they click on it, I create a file and it pops up a file save box allow them to download it to their computer?"

 
You add a link to a PHP script which outputs the correct headers and streams the file.

See the example code and discussion of the "Content-Disposition" header on the PHP online manual page for header()


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
You can make that button to have this onclick function.

onclick="window.location.href='dlcsv.php'"

Then, in your dlcsv.php:

$csvdata = "HERE";
$fn="csv_name.csv";
header("Content-disposition: attachment; filename=$fn");
header("Content-type: inline;");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
echo $csvdata;
unlink ($fn);
 
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=courseReport.csv');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

print($csv);
 
Thanks for all the help. Its working great now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top