0

これは、csv ファイルの作成とそのファイルの強制ダウンロード用です。

これは機能していますが、正しい方法で実行していることを知る必要があります。

public function exportCSV($results){

    try {

        $filename = CSV_PATH.'file.csv';

        $numRows = count($results);

        $handle = fopen($filename, 'w+')  or die("can't open file");

        fputcsv($handle, array('id','name','age','address'));

        foreach ($results as $row) {

            fputcsv($handle, $row);
        }

        fclose($handle);

        header('Content-Disposition: attachment; filename="'.basename($filename).'"');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($filename));
        header('Connection: close');
        readfile($filename);
        exit();

    } catch(Exception $e) {
        echo $e->getMessage(); 
    }
}

CSV_PATH は config.php ファイルにあります

4

1 に答える 1