tags:

views:

14115

answers:

10

Hi,

I have data in MySQL. I am sending the user a URL to get their data out as a CSV.

I have the e-mailing of the link, mysql query, etc covered.

How can I, When they click the link, have a pop-up to download a CVS with the record from MYSQL? I have all the info to get the record already I just dont see how to have PHP create the CSV and let them download a file with a .csv extension.

A: 

To have it send it as a CSV and have it give the file name, use header():

http://us2.php.net/header

header('Content-type: text/csv');
header('Content-disposition: attachment; filename="myfile.csv"');

As far as making the CSV itself, you would just loop through the result set, formatting the output and sending it, just like you would any other content.

Crad
+16  A: 

Try:

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

echo "record1,record2,record3\n";

etc

Oleg Barshay
note the rules for CSVs are important. To ensure good display, put doublequotes around your fields, and don't forget to replace double-quotes inside fields to double double-quotes:`echo '"'.str_replace('"','""',$record1).'","'.str_replace....
Mala
+1  A: 

Create your file then return a reference to it with the correct header to trigger the Save As - edit the following as needed. Put your CSV data into $csvdata.

$fname = 'myCSV.csv';
$fp = fopen($fname,'w');
fwrite($fp,$csvdata);
fclose($fp);

header('Content-type: application/csv');
header("Content-Disposition: inline; filename=".$fname);
readfile($fname);
typemismatch
A: 
A: 

This will do it very easily...

Create .CSV Files From PHP

It prompts the file to the user as you want.

Kevin Korb
A: 

First make data as a String with comma delimiter (separated with ",") ,Something like this

$CSV_string="No,Date,Email,Sender Name,Sender Email \n";//making string, So "\n" is used for newLine

$rand = rand(1,50);// make a random int number between 1 to 50 $file ="export/export".$rand.".csv";// for avoiding cache in Client/Server side it is recommended that the file name be different

file_put_contents($file,$CSV_string);

/* *Or try this code if $CSV_string is an array*
 fh =fopen($file, 'w');
fputcsv($fh , $CSV_string , ","  , "\n" );// *"," is delimiter // "\n" is new line*
fclose($fh);
*/
Behzad Ravanbakhsh
A: 

Simple method -

$data = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"');

$fp = fopen('data.csv', 'w');
foreach($data as $line){
 $val = explode(",",$line);
fputcsv($fp, $val);
}
fclose($fp);

So your each line of $data array will go new line of your newly created csv file. Works only php 5+

A: 

I don't have enough rep yet to comment on the accepted answer, so just pretend this went there. Just to clarify, the correct HTTP Content-Type header for CSV is text/csv, not application/csv. I doubt any modern browser will care either way, but since there are standards we might as well use them.

fentie
A: 

Here is one I have done before, grabbing data between certain dates. Hope it helps.

<?php
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

ini_set('display_errors',1);
$private=1;
error_reporting(E_ALL ^ E_NOTICE);

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$start = $_GET["start"];
$end = $_GET["end"];

$query = "SELECT * FROM customers WHERE created>='{$start} 00:00:00'  AND created<='{$end} 23:59:59'   ORDER BY id";
$select_c = mysql_query($query) or die(mysql_error()); 

while ($row = mysql_fetch_array($select_c, MYSQL_ASSOC))
{
$result.="{$row['email']},";
$result.="\n";
echo $result;
}
?>
Noctine
A: 

Instead of:

$query = "SELECT * FROM customers WHERE created>='{$start} 00:00:00' AND created<='{$end} 23:59:59' ORDER BY id"; $select_c = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($select_c, MYSQL_ASSOC)) { $result.="{$row['email']},"; $result.="\n"; echo $result; }

Use:

$query = "SELECT * FROM customers WHERE created>='{$start} 00:00:00' AND created<='{$end} 23:59:59' ORDER BY id"; $select_c = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($select_c, MYSQL_ASSOC)) { echo implode(",", $row)."\n"; }

Joshua