1

我正在使用 CSV 文件导出 mysql 表。C:既然它直接存储在云端硬盘上而没有任何通知,我如何将其作为下载文件查看

<?php
$host = 'localhost'; // <--  db address
$user = 'root'; // <-- db user name
$pass = 'root'; // <-- password
$db = 'urs'; // db's name
$table = 'veiwresult'; // table you want to export
$file = 'alaa'; // csv name.
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query(" SELECT ApplicantNum,name, averg,choice FROM veiwresult");
fputcsv($f, array('ApplicantNum','name','averg', 'choice'));
$timestamp = date('Ymd-His'); 

$f = fopen("C:/mycsv-{$timestamp}.csv", 'w');
// Headers    
while($row = mysql_fetch_row($result))
{
fputcsv($f, $row);
}


fputcsv($f, $items_array);
fclose($f);

?>
4

2 回答 2

0

如果你想存储 csv 文件,然后不点击就打开它,这是不可能的。

如果您只想打开它,请通过以下方式在浏览器窗口中打开它

header('Content-disposition: inline;filename=foobar.csv');
header('Content-Type: text/csv;charset=UTF-8');
echo $csv_content;

其中 $csv_content 是一个包含 csv 文件内容的字符串。你的猫是这样理解的

$csv_content = file_get_contents('c:/mycsv.csv');
于 2013-01-25T16:29:38.510 回答
0

要下载它...

<?php
$host = 'localhost'; // <--  db address
$user = 'root'; // <-- db user name
$pass = 'root'; // <-- password
$db = 'urs'; // db's name
$table = 'veiwresult'; // table you want to export
$file = 'alaa'; // csv name.
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query(" SELECT ApplicantNum,name, averg,choice FROM veiwresult");
fputcsv($f, array('ApplicantNum','name','averg', 'choice'));
$timestamp = date('Ymd-His'); 

$f = fopen("C:/mycsv-{$timestamp}.csv", 'w');
// Headers    
while($row = mysql_fetch_row($result))
{
fputcsv($f, $row);
}

fputcsv($f, $items_array);
fclose($f);

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="mycsv-{$timestamp}"');
readfile('C:/mycsv-{$timestamp}.csv');
?>

这仍会通知用户下载,并且根据他们的设置,他们可能需要接受下载。这个功能当然不能被覆盖。

于 2013-01-25T17:07:21.073 回答