我的php代码将.csv文件保存在服务器上。当我直接从服务器获得。csv文件时,它的内容很好,我可以在Excel中查看它。但是在我的网页中,当我点击保存文件时,保存在我电脑上的内容是html文件的内容,其次是csv文件的内容。会出什么问题呢?
这里是我有问题的代码:
while ($row = oci_fetch_array($my_query, OCI_ASSOC+OCI_RETURN_NULLS))
{
//store content in csv format into $mymessage
}
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $mymessage);
fclose($handle);
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=$my_file");
readfile("$my_file");
exit;
============================================================
内容如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252">
<title>ABCDEF...</title>
</head>
<body>
<font size="4" color=blue><a><b><i><u>12345678</u></i></b></a></font>
<br></br>
...
CSV REPORT EXTRACTED AS OF, 2014/03/25 08:18:16
SERVER,APP-SERVER
server1,weblogic
server2,jboss
server3,weblogic
server4,weblogic
server5,websphere
...
提前欣赏。
这似乎是某种路由问题。收取CSV数据的php页面应该是唯一被调用的。但似乎你正在使用一个框架或你可能有一个包含或你有重写在你的服务器上。
好了,我解决了。我必须在$handle()和first header()之前添加ob_end_clean()。完成后,所有缓冲的输出都被清除,csv文件被保存,没有"额外"内容。
感谢你花时间帮助我。