使用 PHP 创建和下载 CSV
我正在使用 PHP 从数据库查询创建 CSV 文件。 我运行查询,设置标题,然后在 Firefox 中加载页面,文件提示下载并在 Excel 中打开,就像预期的那样。 当我在 IE 中尝试时,出现错误:Internet Explorer 无法从 www.website.com 下载 ReportPrint.php。
Internet Explorer 无法打开此 Internet 站点。请求的站点不可用或无法找到。请稍后重试。
不知道可以采取什么措施来解决这个问题。
header('Content-Description: File Transfer');
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename=Report.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "record1,record2,record3\n";
I'm using PHP to create a CSV file from a database query.
I run the query, set the headers, and load the page up in Firefox and the file promps for download and opens in excel just like it's supposed to.
When I try it in IE, I get an error saying Internet Explorer cannot download ReportPrint.php from www.website.com.
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.
Not sure what can be done to fix this.
header('Content-Description: File Transfer');
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename=Report.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "record1,record2,record3\n";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当 Internet Explorer 无法缓存文件并且您似乎试图避免缓存时,它往往会显示这些错误消息。请尝试这样的操作:
根据您的喜好调整
$seconds
但不要将其设置为零。它还有助于使用
mod_rewrite
让 IE 认为下载的是静态文件,例如http://example.com/Report.csv
请报告回来并告知如果它适合你。
Internet Explorer tends to display these error messages when it's not able to cache the file and you appear to be trying to avoid caching. Try instead something like this:
Adjust
$seconds
to your liking but don't set it to zero.It also helps to use
mod_rewrite
to make IE believe that the download is a static file, e.g.,http://example.com/Report.csv
Please report back and tell if it works for you.
删除
Pragma: no-cache
标头。该标头阻止 IE 下载该文件。Remove the
Pragma: no-cache
header. This header prevents IE from downloading the file.检查 IE 的安全设置,可能是它配置为不下载 CSV 文件。
Check the security settings of IE, may be it's configured not to download CSV files.
更改
为
Change
to