将CSV读入PHP,对其进行处理,然后立即下载处理后的输出[关闭]

问题描述 投票:0回答:1

我正在尝试使用将CSV加载到PHP数组,然后对其进行处理,并将该过程的结果输出到html文件中,该文件将立即下载。我已经完成了读入和处理的工作,但是无法使下载工作。我找到了https://www.php.net/manual/en/function.readfile.php,但是所有示例都显示了创建文件然后下载的过程,我可以将缓冲区中的HTML代码直接写入download.html文件吗?任何帮助表示赞赏。马库斯

php html csv
1个回答
0
投票

喜欢这个,你可以做:

<?php    
    $html_string.="<h1>This is your form</h1>";
    foreach ($_POST as $p=>$value) $html_string.="{$p} has {$value}";

    header('Content-Disposition: attachment; filename="readme.html"');
    // other header() calls you need
    die($html_string);    
?>


$f = fopen("tmp.csv", "w");
foreach ($array as $line) {
    fputcsv($f, $line)
}

header('Content-Disposition: attachment; filename="filename.csv";');
© www.soinside.com 2019 - 2024. All rights reserved.