我在MySQL数据库中有数据。我向用户发送一个URL,以获取他们的数据作为CSV文件。
我有链接的电子邮件,MySQL查询等覆盖。
当他们点击链接时,如何弹出从MySQL下载带有记录的CVS的窗口?
我已经有了所有能拿到唱片的资料。我只是不明白如何让PHP创建CSV文件,并让他们下载一个扩展名为. CSV的文件。
我在MySQL数据库中有数据。我向用户发送一个URL,以获取他们的数据作为CSV文件。
我有链接的电子邮件,MySQL查询等覆盖。
当他们点击链接时,如何弹出从MySQL下载带有记录的CVS的窗口?
我已经有了所有能拿到唱片的资料。我只是不明白如何让PHP创建CSV文件,并让他们下载一个扩展名为. CSV的文件。
当前回答
首先将data作为String,以逗号作为分隔符(用“,”分隔)。就像这样
$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 the client and on the 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);
*/
其他回答
除了上述内容,你可能还需要补充:
header("Content-Transfer-Encoding: UTF-8");
它在处理包含多种语言的文件时非常有用,比如人名或城市。
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
function outputCSV($data) {
$output = fopen("php://output", "wb");
foreach ($data as $row)
fputcsv($output, $row); // here you can change delimiter/enclosure
fclose($output);
}
outputCSV(array(
array("name 1", "age 1", "city 1"),
array("name 2", "age 2", "city 2"),
array("name 3", "age 3", "city 3")
));
php: / /输出 函数
编写自己的CSV代码可能是浪费你的时间,只是使用一个包,如league/ CSV -它为你处理所有困难的事情,文档是很好的,它是非常稳定/可靠的:
http://csv.thephpleague.com/
你需要使用作曲家。如果你不知道什么是作曲家,我强烈建议你去看看:https://getcomposer.org/
公共函数actionExportnotificationresponselogdata() {
$fileName = '/tmp/notificationresponselogs_' . date('d-m-Y-g-i-h') . '.csv';
$f = fopen($fileName, 'w');
fputs( $f, "\xEF\xBB\xBF" ); //for utf8 support in csv
$csv_fields=array();
$csv_fields[] = 'heading1';
$csv_fields[] = 'heading2';
$csv_fields[] = 'heading3';
$csv_fields[] = 'heading4';
$csv_fields[] = 'heading5';
$csv_fields[] = 'heading6';
$csv_fields[] = 'heading7';
$csv_fields[] = 'heading8';
fputcsv($f, $csv_fields);
$notification_log_arr = $notificationObj->getNotificationResponseForExport($params); //result from database
if (count($notification_log_arr) > 0) {
$serialNumber=1;
foreach ($notification_log_arr AS $notifaction) {
$fields = array();
$fields['serialNumber']= $serialNumber ;
$fields['fld_1']= $notifaction['fld_1'] ;
$fields['fld_2']= $notifaction['fld_2'] ;
$fields['fld_3']= $notifaction['fld_3'] ;
$fields['fld_4']= $notifaction['fld_4'] ;
$fields['fld_5']= $notifaction['fld_5'] ;
$fields['fld_6']= $notifaction['fld_6'] ;
$fields['fld_7']= $notifaction['fld_7'] ;
// print_r($fields); die;
fputcsv($f, $fields,",");
$serialNumber++; }
fclose($f);
if (file_exists($fileName)) {
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename="."exportlog".date("Y-m-d_H:i").".csv");
header("Content-length: " . filesize($fileName));
header("Pragma: no-cache");
header("Expires: 0");
readfile($fileName);
unlink($fileName);
exit;
}
}
首先将data作为String,以逗号作为分隔符(用“,”分隔)。就像这样
$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 the client and on the 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);
*/