使用 http-header 强制下载字符串

发布于 2024-07-19 08:12:06 字数 550 浏览 2 评论 0原文

我的功能有问题。 我的函数的目的是强制下载字符串。 真正发生的情况是字符串被输出到屏幕上而不是下载。

这是我的功能:

function arrayToCSV($vectorDados, $cabecalho)
{
    $arr = array();
    $arr=$vectorDados;

    $csv = $cabecalho . "\n";
    foreach($arr as $row) {
        $csv .=$row[0] . "  " .  $row[1] . "    ". $row[2] . "  " .$row[3] . "  \n";
    }
    $filename = "emails_".date("Y-m-d_H-i",time());


    header ("Content-Type: application/octet-stream");
    header ("Content-disposition: attachment; filename=".$filename.".csv");
    print $csv;
}

I'm having a problem with my function.
The purpose of my function is to force a string to be downloaded. What it really happens is that the string is outputted to the screen and not downloaded.

This is my function:

function arrayToCSV($vectorDados, $cabecalho)
{
    $arr = array();
    $arr=$vectorDados;

    $csv = $cabecalho . "\n";
    foreach($arr as $row) {
        $csv .=$row[0] . "  " .  $row[1] . "    ". $row[2] . "  " .$row[3] . "  \n";
    }
    $filename = "emails_".date("Y-m-d_H-i",time());


    header ("Content-Type: application/octet-stream");
    header ("Content-disposition: attachment; filename=".$filename.".csv");
    print $csv;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

〗斷ホ乔殘χμё〖 2024-07-26 08:12:06

问题不在于功能。

通过将 ob_start() 添加到主代码中,现在我我能够下载我的字符串。

The problem was not with the function.

By adding ob_start() to the main code, now I'm able to download my string.

注定孤独终老 2024-07-26 08:12:06

尝试使用大写 D 的 Content-Disposition

(内容类型可能是 text/csv)

Try Content-Disposition with a capital D.

(and maybe text/csv for the Content-Type)

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