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


allow download using fputcsv but leaving out extra html code in outputted file?

在我的代码中,我使用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->filepointer = fopen('file.csv', 'w'),文件仍然会被填充html。因为我不需要下载文件,我只是用它来检查文件是否以正确的格式创建。再次感谢

您可以尝试下面的代码。在设置一些头文件后,需要在末尾添加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
?>

使用ob_end_clean();在写入文件

之前