使用 PHP 创建和下载 CSV

发布于 2024-09-19 11:23:20 字数 527 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温馨耳语 2024-09-26 11:23:20

当 Internet Explorer 无法缓存文件并且您似乎试图避免缓存时,它往往会显示这些错误消息。请尝试这样的操作:

<?php

$seconds = 30;

header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$seconds) . ' GMT');
header('Cache-Control: max-age=' . $seconds . ', s-maxage=' . $seconds . ', must-revalidate, proxy-revalidate');

session_cache_limiter(FALSE); // Disable session_start() caching headers
if( session_id() ){
    // Remove Pragma: no-cache generated by session_start()
    if( function_exists('header_remove') ){
        header_remove('Pragma');
    }else{
        header('Pragma:');
    }
}

?>

根据您的喜好调整 $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:

<?php

$seconds = 30;

header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$seconds) . ' GMT');
header('Cache-Control: max-age=' . $seconds . ', s-maxage=' . $seconds . ', must-revalidate, proxy-revalidate');

session_cache_limiter(FALSE); // Disable session_start() caching headers
if( session_id() ){
    // Remove Pragma: no-cache generated by session_start()
    if( function_exists('header_remove') ){
        header_remove('Pragma');
    }else{
        header('Pragma:');
    }
}

?>

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.

绅刃 2024-09-26 11:23:20

删除 Pragma: no-cache 标头。该标头阻止 IE 下载该文件。

Remove the Pragma: no-cache header. This header prevents IE from downloading the file.

路还长,别太狂 2024-09-26 11:23:20

检查 IE 的安全设置,可能是它配置为不下载 CSV 文件。

Check the security settings of IE, may be it's configured not to download CSV files.

失去的东西太少 2024-09-26 11:23:20

更改

header("Content-Disposition: attachment; filename=Report.csv");

header("Content-Disposition: attachment;filename=Report.csv");

Change

header("Content-Disposition: attachment; filename=Report.csv");

to

header("Content-Disposition: attachment;filename=Report.csv");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文