我有一些PHP代码,可以在数据库上运行查询,将结果保存到csv文件中,然后允许用户下载该文件。问题是,csv文件包含围绕实际csv内容的页面HTML。
我已经看过这里所有的相关问题,包括
this one
. 不幸的是,我的代码存在于JoOMLA中,所以即使我试图重定向到一个包含头的任何页面,JooMLA都会自动地用它自己的导航代码包围它。这只在下载时发生;如果我查看保存在服务器上的csv文件,它不包含HTML。
有谁能帮我找到一种方法,强制下载服务器上的实际csv文件,而不是浏览器正在编辑它?我试过使用标题位置,如下所示:
header('Location: ' . $filename);
但它在浏览器中打开文件,而不是强制保存对话框。
这是我当前的代码:
$filename = "customers.csv";
$fp = fopen($filename, 'w');
$query = "select
c.firstname,c.lastname,c.email as customer_email,
a.email as address_email,c.phone as customer_phone,
a.phone as address_phone,
a.company,a.address1,a.address2,a.city,a.state,a.zip, c.last_signin
from {$dbpre}customers c
left join {$dbpre}customers_addresses a on c.id = a.customer_id order by c.last_signin desc";
$votes = mysql_query($query) or die ("File: " . __FILE__ . "<br />Line: " . __LINE__ . "<p>{$query}<p>" . mysql_error());
$counter = 1;
while ($row = mysql_fetch_array($votes,1)) {
if ($counter == 1){
$headerRow = array();
foreach ($row as $key => $val)
$headerRow[] = $key;
fputcsv($fp, $headerRow);
}
fputcsv($fp, $row);
$counter++;
}
fclose($fp);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Transfer-Encoding: binary");
readfile($filename);
exit;
完整的URL如下所示:
http:
实际下载链接如下:
http://mysite.com/administrator/index.php?option=com_eimcart&task=customers&subtask=export
更多编辑
index.php?option=com_eimcart&task=customers&subtask=export&format=raw
下面是生成的保存文件的屏幕截图:
$fp= fopen("php://output", 'w');
$query = "select c.firstname,c.lastname,c.email as customer_email,
a.email as address_email,c.phone as customer_phone,
a.phone as address_phone, a.company, a.address1,
a.address2,a.city,a.state,a.zip,c.last_signin
from {$dbpre}customers c
left join {$dbpre}customers_addresses a on c.id = a.customer_id
order by c.last_signin desc";
$votes = mysql_query($query) or die ("File: " . __FILE__ . "<br />Line: " . __LINE__ . "<p>{$query}<p>" . mysql_error());
$counter = 1;
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=customers.csv");
header("Content-Transfer-Encoding: binary");
while ($row = mysql_fetch_array($votes,1)) {
if ($counter == 1){
$headerRow = array();
foreach ($row as $key => $val)
$headerRow[] = $key;
fputcsv($fp, $headerRow);
}
fputcsv($fp, $row);
$counter++;
}
fclose($fp);
BJORN的更新
这是对我有用的代码。在调用操作的链接中使用原始参数:
index.php?option=com_eimcart&task=customers&subtask=export&format=raw
因为这是过程性的,所以我们的链接位于一个名为customers.php的文件中,该文件如下所示:
switch ($r['subtask']){
case 'add':
case 'edit':
include("subnav.php");
if ($r['custFormSubmitted'] == "true")
include("validate.php");
else
include("showForm.php");
break;
case 'delete':
include("subnav.php");
include("process.php");
break;
case 'resetpass':
include("subnav.php");
include("resetpassword");
break;
case 'export':
include("export_csv.php");
break;
default:
include("subnav.php");
include("list.php");
break;
}
因此,当用户单击上面的链接时,会自动包含export_csv.php文件。该文件包含所有实际代码:
<?
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=customers.csv");
header("Content-Transfer-Encoding: binary");
$fp= fopen("php://output", 'w');
$query = "select
c.firstname,c.lastname,c.email as customer_email,
a.email as address_email,c.phone as customer_phone,
a.phone as address_phone,
a.company,a.address1,a.address2,a.city,a.state,a.zip, c.last_signin
from {$dbpre}customers c
left join {$dbpre}customers_addresses a on c.id = a.customer_id order by c.last_signin desc";
$votes = mysql_query($query) or die ("File: " . __FILE__ . "<br />Line: " . __LINE__ . "<p>{$query}<p>" . mysql_error());
$counter = 1;
while ($row = mysql_fetch_array($votes,1)) {
if ($counter == 1){
$headerRow = array();
foreach ($row as $key => $val)
$headerRow[] = $key;
fputcsv($fp, $headerRow);
}
fputcsv($fp, $row);
$counter++;
}
fclose($fp);