LoginSignup
8
6

More than 5 years have passed since last update.

【PHP】CSVのダウンロードって

Posted at

こんにちは:whale2:
CSV ダウンロードページって昔は文字化けに苦しめられていたイメージがありますが、最近はどうなのでしょう。

ソースコード

以下のような感じで、Windows / Mac で化けませんでした。

CSVダウンロードページ
<?php
error_reporting( -1 ) ;
ini_set( 'display_errors', 1 ) ;

$csv_text = '㍊,㍻,髙,靍,濵,﨑,①,Ⅳ' ;
$csv_file_name = 'やゔぁい文字が含まれた.csv' ;


header('Content-Type: text/csv') ;
header('Content-Disposition: attachment; filename=' . $csv_file_name . "; filename*=UTF-8''" . urlencode($csv_file_name)) ;
echo pack('C*',0xEF,0xBB,0xBF) . $csv_text ;

おわり

Content-Type に application/vnd.ms-excel とか application/octet-stream とか指定したり、
mb_convert_encoding()SJIS-winに変換したり、
Mac版ExcelのためにBOM付きUTF-16LEに変換したり、
ファイル名はアルファベットだけに制限したり、
…する時代は終わったのだろうか1

すてき:cherry_blossom: 2

参考と注釈


  1. しかしApache OpenOfficeでは開けないという噂をきいたことがある。ほんと? 

  2. 詳しい人達に後ろから刺されたい。:bow_and_arrow: 

8
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6