【发布时间】:2010-11-14 21:41:43
【问题描述】:
我已经看到了一篇关于此的帖子,但它并没有真正提供解决方案(至少对我有用)...
我有一个 PHP 页面,它执行一些基本的 MySQL 查询,其结果显示在页面上。我在整个过程中使用了一些 $_GET 和 $_SESSION 变量。
在同一页面中,我还允许用户“导出为 CSV”(通过调用函数)。从导出返回的文件在底部有 CSV 结果,但还包含我的页面的 HTML(它调用该函数)。
现在,在页面顶部我有 ob_start(),在底部我有 ob_flush(),否则在页面加载时我会收到一些“无法修改标题...”错误。因此,正如我阅读的帖子中所建议的那样:
My guess is that you've got some sort of template that generates the same HTML header and footer regardless of the page that is requested. Sometime before the exportCSV function is called, the header is generated.
You don't show the bottom of the output, but I'll bet the footer is there too, since I suspect the flow control will continue on to that code after the function exits."
(http://stackoverflow.com/questions/207019/why-am-i-getting-html-in-my-mysql-export-to-csv/207046)
有人对我如何防止这种情况发生有任何想法吗?如果我应该发布一些代码,请告诉我,我会...
谢谢!
编辑:
在调用导出函数之前调用 ob_end_clean() 时,它会删除 CSV 之前的任何 html。但是,在 CSV 结果之后,我仍然会收到一些 HTML 结束标记...
fname lname MONTH WeekdayCount WeekendCount
John Doe 8 1 0
John Doe 7 3 2
Jane Smith 7 3 2
</div>
</div>
</div>
</body>
</html>
编辑:
此问题已通过在调用 CSV 导出脚本后调用 exit() 解决。
【问题讨论】: