PHP不会将CSV下载到浏览器

时间:2017-10-25 19:10:27

标签: php

我似乎无法将CSV文件下载到浏览器...有一个SQL查询。

$result = $conn->query($sql);

if (mysqli_num_rows($result) > 0) {
    $counter=1;

    // open file and put herder to csv file
    $file = fopen('php://output', 'w');
    fputcsv($file, array('id','name','test1','test2'));
    // put content to csv file
    while ($row = mysqli_fetch_assoc($result)) {

        fputcsv($file, array($counter++,$row['name'],
                             ($row['test1']!=-1)?$row['test1']:'null',($row['test2']!=-1)?$row['test2']:'null'));
    }
    $csv_name=(isset($_GET['device'])&&$_GET['device']!='')?$_GET['device']:"all_devices";
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment;'.
           'filename='.$csv_name.'_'.date('Y-m-d').'.csv');
}else{
    echo "No device found";
}

0 个答案:

没有答案