php 强制下载 CSV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17269568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:35:34  来源:igfitidea点击:

Force Download CSV File

phpcsv

提问by MONZTAAA

I am trying to create a button that will force a CSV file download but for some reason I am not able to get it working. This is the code that I have:

我正在尝试创建一个按钮来强制下载 CSV 文件,但由于某种原因我无法让它工作。这是我拥有的代码:

public function export_to_csv($result) {

$shtml="";

for ($i=0; $i<count($result); $i++){

$shtml = $shtml.$result[$i]["order_number"]. "," .$result[$i]["first_name"]. "," .$result[$i]["middle_name"]. "," .$result[$i]["last_name"]. "," .$result[$i]["email"]. "," .$result[$i]["shipment_name"]. "," .$result[$i]["payment_name"]. "," .$result[$i]["created_on"]. "," .$result[$i]["modified_on"]. "," .$result[$i]["order_status"]. "," .$result[$i]["order_total"]. "," .$result[$i]["virtuemart_order_id"]. "\n";

}

Header('Content-Description: File Transfer');
Header('Content-Type: application/force-download');
Header('Content-Disposition: attachment; filename=pedidos.csv');

}

The $result variable comes from here:

$result 变量来自这里:

public function get_orders_k() {

$db=JFactory::getDBO();

    $query = " select o.order_number, o.virtuemart_order_id, o.order_total, o.order_status, o.created_on, o.modified_on, u.first_name,u.middle_name,u.last_name "
    .',u.email, pm.payment_name, vsxlang.shipment_name ' .
    $from = $this->getOrdersListQuery();

$db->setQuery($query);

$result=$db->loadAssocList();

    if ( $result > 0 )
    {
        $datos = VirtueMartModelKiala::export_to_csv($result);

    }else return 0;
}

Im not sure even where to start looking. I have look over the internet on various ways of doing this and tried all of them and still can't manage to get it working. Please help me out!

我什至不确定从哪里开始寻找。我已经在互联网上查看了执行此操作的各种方法并尝试了所有方法,但仍然无法使其正常工作。请帮帮我!

-Thanks

-谢谢

回答by Robert

Put this code below your loop.

将此代码放在循环下方。

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="pedidos.csv"');

echo $shtml;

How did you find out the content type application/force-download? I've never heard of it. The proper MIME type for CSV is text/csv

您是如何找到内容类型应用程序/强制下载的?我从来没听说过。CSV 的正确 MIME 类型是text/csv

You can find more examples how to use header function in php manual

您可以在php 手册中找到更多如何使用 header 函数的示例

You need also display $shtmlyou didn't do it in your code.

您还需要$shtml在代码中显示您没有这样做。

回答by The Alpha

Well. you may try this, it'll work if your result is not empty

好。你可以试试这个,如果你的结果不为空它会起作用

public function export_to_csv($result)
{
    if(!$result) return false;
    ob_end_clean();
    header( 'Content-Type: text/csv' );
    header( 'Content-Disposition: attachment;filename=pedidos.csv');
    $fp = fopen('php://output', 'w');
    $headrow = $result[0];
    fputcsv($fp, array_keys($headrow));
    foreach ($result as $data) {
        fputcsv($fp, $data);
    }
    fclose($fp);
    $contLength = ob_get_length();
    header( 'Content-Length: '.$contLength);
}

回答by DevZer0

you need to echoyour content after the header

您需要echo在标题之后查看您的内容

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename=pedidos.csv');
echo $shtml