0

次のように、phpでcsv形式のファイルをダウンロードする関数を作成しました:-

function report_download_csv($fields, $data, $filename) {
        global $CFG;
require_once($CFG->dirroot.'/user/profile/lib.php');

    $filename = clean_filename($filename.'-'.date('Y-m-d').'.csv');
    header("Content-Type: application/download\n");
    header("Content-Disposition: attachment; filename=$filename");
    header("Expires: 0");
    header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
    header("Pragma: public");

    $row = array(); 
        $date_field = array('registrationdate', 'expirydate', 'startdate', 'enddate');
        $totalrow = count($fields);
        $row[] = implode(',',$fields)."\n";

        foreach($data as $d){
            for($i=0;$i<$totalrow;$i++){
                    $row_d[] = $d->$fields[$i];
            }
            $row[] = implode(',',$row_d);
            $row_d = array();
        }
        echo implode($row, "\n");
    die;
}

ただし、関数を呼び出すと、ブラウザに結果が表示されるだけです。関数で変更する必要があるものはありますか?

4

2 に答える 2

1

このようにしてみてください..

header('Content-Type: application/csv');
header('Pragma: no-cache');
于 2012-10-22T06:18:12.890 に答える
0

次のヘッダーはダウンロードを強制する必要があります

header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
于 2012-10-22T06:16:57.260 に答える