PHPでCSVダウンロード
PHPでCSVダウンロードする際のモジュール
UTF-8で出力する場合
function export($file_name, $data)
{
    $fp = fopen('php://output', 'w');
    
    foreach ($data as $row) {
        fputcsv($fp, $row, ',', '"');
    }
    fclose($fp);
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename={$file_name}");
    header('Content-Transfer-Encoding: binary');
    exit;
}
SJIS-winで出力する場合
function export($file_name, $data)
{
    $fp = fopen('php://output', 'w');
    
    // UTF-8からSJIS-winへ変換するフィルター
    stream_filter_append($fp, 'convert.iconv.UTF-8/CP932//TRANSLIT', STREAM_FILTER_WRITE);
    
    foreach ($data as $row) {
        fputcsv($fp, $row, ',', '"');
    }
    fclose($fp);
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename={$file_name}");
    header('Content-Transfer-Encoding: binary');
    exit;
}