允许使用fputcsv下载,但在输出文件中省略了额外的HTML代码?

时间:2013-03-11 16:21:38

标签: php html fputcsv

在我的代码中,我正在用fgetcsv创建一个文件。我下载并打开文件,但我的页面中的所有html都包含在csv文件中。无论如何,我可以只从文件中输出要输出的数据而不是额外的html。我创建csv的功能在

之下
function makefile()
{
    header('Content-type: text/csv');
    header("Cache-Control: no-store, no-cache"); 
    header("Content-Disposition: attachment;filename=file.csv");

    $this->filepointer  =   fopen('php://output', 'a');



    for($count=0;$count < count($this->alldata);$count++)
    {
       fputcsv($this->filepointer, $this->alldata[$count]);
    }

    fclose($this->filepointer);

}

我也想知道如果我使用$ this-&gt; filepointer = fopen('file.csv','w')文件仍然会用html填充。因为我没有文件下载我只是用它来检查文件是否以正确的格式创建。再次感谢

2 个答案:

答案 0 :(得分:0)

使用ob_end_clean();在写文件之前

答案 1 :(得分:0)

您可以尝试以下代码。在设置了一些标题后,你需要在最后添加read_file。

<?php
if(isset($_GET['link']))
{
    $var_1 = $_GET['link'];
    $file = $var_1;

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));
    ob_clean();
    flush();
    readfile($file);
    exit;
    }
} //- the missing closing brace
?>