使用 .csv 导出数组

Export an array in .csv using

我的 .csv 有问题。所以我尝试使用 php 从数组生成 .csv。在我看来:

<form id="form_logistique" action="myroute" method="post">
            <div class="form-group " style="float: left;">
                <label class="control-label" for="" style="display: inline-block; padding-right: 20px;">Date min</label>
                <input type="text" id="date_min" name="date_min.name" value="date_min" placeholder="Date min" class="form-control datepicker"  />
            </div>

            <div class="form-group" style="float: left;padding-left: 20px;">
                <label class="control-label" for="" style="display: inline-block; padding-right: 20px;">Date max</label>
                <input type="text" id="date_max" name="date_max" value="{{ date_max" placeholder="Date max" class="form-control datepicker"  />
            </div>


            <input type="submit" class="btn btn-primary" style="margin-top: 25px;margin-left: 20px;" value="Rechercher"></input>
            <input type="submit" class="btn btn-succes" style="margin-top: 25px;margin-left: 20px;" name="export" value="Exporter"></input>
    </form>

在 php 中:

 public function getLogistique()
{
    $this->form_logistique = new Form\Form(
        new Form\Field\Text('date_min', '', true),
        new Form\Field\Text('date_max','',true),
    );
    $date_min = '';
    $date_max = '';
    if ($this->getRequest()->isPostMethod() && $this->form_logistique->bind($_POST)){
        $date_min = $this->form_logistique->date_min->getValue();
        $date_max = $this->form_logistique->date_max->getValue();
    }
    $interdit  = array(";", ",", ":", "*", "/", "|", "?", '"', "<", ">", "!", "_", "@", "[", "]", "\", "{", "}", "~");
    $aGifts = Gain::getGiftForLogistique($date_min, $date_max, $statut);
    foreach($aGifts as $gift){
        $date = explode(' ', $gift['date_gain']);
        $gift['ref_article']        = $gift['ref_article'];
        $gift['nom']                = str_replace($interdit,"",$gift['nom']);
        $gift['prenom']             = str_replace($interdit,"",$gift['prenom']);
        $gift['pseudo']             = $gift['pseudo'];
        $gift['numero']             = trim(str_replace(";",",",str_replace("\"," ",$gift['numero'])));
        $gift['rue']                = str_replace($interdit,"",$gift['rue']);
        $gift['complement']         = str_replace($interdit,"",$gift['complement']);
        $gift['code_postal']        = $gift['code_postal'];
        $gift['ville']              = str_replace(";",",",str_replace("\"," ",$gift['ville']));
        $gift['pays']               = $gift['pays'];
        $gift['email']              = Gain::getEmailByIdm($gift['pseudo']);
        $gift['tel']                = str_replace(";",",",str_replace("\"," ",$gift['tel']));
        $gift['id_instant_gagnant'] = $gift['id_instant_gagnant'];
        $gift['date_gain']          = $date[0];
        $aFilterGifts[] = $gift;
    }
    $this->aFilterGifts = $aFilterGifts;
    if (isset($_POST['export'])) {
        $output = fopen('php://output', 'w');
        $sFileName = 'Fichier_de_logistique.csv';
        header('Content-Disposition: attachement; filename="' . $sFileName . '";');
        header('Content-Type: application/download');
        fwrite($output, "sep=;\n");
        fputcsv($output, array(''Nom', 'Prenom'), ";");
        foreach ($aFilterGifts as $value) {
            fputcsv($output, $value, ";");
        }
        fpassthru($output);
        fclose($output);
    }
    return $this->render('template/customer_service/member/logistique.twig');
}

.csv 已生成,但问题是在 .csv 中的数组之后我拥有页面的所有内容。html 我不明白 problem.Please 在哪里帮助我!提前致谢

您的问题不是很清楚,可能是您想在 fclose() 之后终止脚本的执行。

这是因为在代码中的某些时候您可能会回显或打印一些东西。更好的方法是将 csv 写入文件。然后将该文件作为 Content-Disposition: 附件发送。就像在下面的代码中,file 是文件的路径。

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}

fputcsv($output, array(''Nom', 'Prenom'), ";");

此处输入错误 - Nom 前的双单引号。

问题出在这里:

if (isset($_POST['export'])) {
    $output = fopen('php://output', 'w');
    $sFileName = 'Fichier_de_logistique.csv';
    header('Content-Disposition: attachement; filename="' . $sFileName . '";');
    header('Content-Type: application/download');
    fwrite($output, "sep=;\n");
    fputcsv($output, array('Nom', 'Prenom'), ";");
    foreach ($aFilterGifts as $value) {
        fputcsv($output, $value, ";");
    }
    fpassthru($output);
    fclose($output);
}
return $this->render('template/customer_service/member/logistique.twig');

该函数将 headers 和 CSV 文件的内容写入 STDOUT (php://output),但随后整个马戏团继续进行。这个函数 returns 模板的内容到它的 parent 函数,这也可能将它渲染到 STDOUT(使用 echoprint 或其他)。在这里最简单的做法(但不是正确的)是将 die(); 放在 fclose($output); 之后:

if (isset($_POST['export'])) {
    $output = fopen('php://output', 'w');
    $sFileName = 'Fichier_de_logistique.csv';
    header('Content-Disposition: attachement; filename="' . $sFileName . '";');
    header('Content-Type: application/download');
    fwrite($output, "sep=;\n");
    fputcsv($output, array('Nom', 'Prenom'), ";");
    foreach ($aFilterGifts as $value) {
        fputcsv($output, $value, ";");
    }
    fpassthru($output);
    fclose($output);
    die();
}
return $this->render('template/customer_service/member/logistique.twig');

我认为正确的方法是为 CSV 导出创建一个新的路由和控制器操作,没有 HTML 输出。