我在導出使用php從我的一個mysql表創建的csv文件時遇到了一些麻煩。從Mysql導出CSV
我正在使用的代碼會打印正確的數據,但我看不到如何在csv文件中下載此數據,並提供下載鏈接到創建的文件。我以爲瀏覽器應該自動提供下載文件,但它不會。 (難道是因爲下面的代碼使用AJAX叫什麼名字?)
任何幫助,不勝感激 - 下面的代碼,S.
include('../cofig/config.php'); //db connection settings
$query = "SELECT * FROM isregistered";
$export = mysql_query($query) or die("Sql error : " . mysql_error());
$fields = mysql_num_fields($export);
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
while ($row = mysql_fetch_row($export)) {
$line = '';
foreach ($row as $value) {
if ((!isset($value)) || ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line) . "\n";
}
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
//header("Content-type: application/octet-stream"); //have tried all of these at sometime
//header("Content-type: text/x-csv");
header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=export.csv");
//header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo '<a href="">Download Exported Data</a>'; //want my link to go in here...
print "$header\n$data";
非常感謝大家的幫助 - 非常感謝。 – ss888 2011-01-06 10:49:19