質問するログイン新規登録

回答編集履歴

5

修正

2019/01/28 20:34

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -39,11 +39,6 @@
39
39
 
40
40
  }
41
41
 
42
- // バッファリングを必ず解除する
43
- while (ob_get_level()) {
44
- ob_end_clean();
45
- }
46
-
47
42
  // ヘッダ出力
48
43
  header("Content-Type: text/csv; charset=Windows-31J");
49
44
  header("Content-Disposition: attachment; filename=export.tsv");

4

修正

2019/01/28 20:34

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -24,7 +24,6 @@
24
24
  [
25
25
  \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
26
26
  \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
27
- \PDO::ATTR_EMULATE_PREPARES => true,
28
27
  \PDO::ATTR_TIMEOUT => 10,
29
28
  \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false,
30
29
  ]

3

修正

2019/01/28 20:30

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -46,7 +46,7 @@
46
46
  }
47
47
 
48
48
  // ヘッダ出力
49
- header("Content-Type: text/csv; charset=UTF-8");
49
+ header("Content-Type: text/csv; charset=Windows-31J");
50
50
  header("Content-Disposition: attachment; filename=export.tsv");
51
51
 
52
52
  // ストリームの用意
@@ -54,7 +54,6 @@
54
54
  stream_filter_prepend($stream, 'convert.iconv.utf-8/cp932');
55
55
 
56
56
  // 見出しを書き込む
57
- fwrite($stream, "\xEF\xBB\xBF");
58
57
  fputcsv($stream, ["ID"]);
59
58
 
60
59
  // 各行を書き込む

2

追記

2019/01/28 20:26

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -5,4 +5,60 @@
5
5
  `$stream = fopen('php://output', 'wb');`
6
6
 
7
7
  - `php://temp` はテンポラリメモリ(書き込んだあと読み出さないなら何の意味もない)
8
- - `php://output` は mod_php や php-fpm におけるHTTPのボディパート出力(`echo` で出すのと同じ場所)
8
+ - `php://output` は mod_php や php-fpm におけるHTTPのボディパート出力(`echo` で出すのと同じ場所)
9
+
10
+ ----
11
+
12
+ 【追記】
13
+
14
+ これでお試しください↓
15
+
16
+ ```php
17
+ <?php
18
+
19
+ try {
20
+
21
+ $pdo = new PDO('mysql:dbname=test;host=IP;port=22;charset=utf8mb4',
22
+ 'root',
23
+ 'password',
24
+ [
25
+ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
26
+ \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
27
+ \PDO::ATTR_EMULATE_PREPARES => true,
28
+ \PDO::ATTR_TIMEOUT => 10,
29
+ \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false,
30
+ ]
31
+ );
32
+
33
+ $stmt = $pdo->query("SELECT * FROM `log`");
34
+
35
+ } catch (\PDOException $e) {
36
+
37
+ // デバッグ用
38
+ header("Content-Type: text/plain; charset=UTF-8", true, 500);
39
+ exit($e->getMessage());
40
+
41
+ }
42
+
43
+ // バッファリングを必ず解除する
44
+ while (ob_get_level()) {
45
+ ob_end_clean();
46
+ }
47
+
48
+ // ヘッダ出力
49
+ header("Content-Type: text/csv; charset=UTF-8");
50
+ header("Content-Disposition: attachment; filename=export.tsv");
51
+
52
+ // ストリームの用意
53
+ $stream = fopen('php://output', 'wb');
54
+ stream_filter_prepend($stream, 'convert.iconv.utf-8/cp932');
55
+
56
+ // 見出しを書き込む
57
+ fwrite($stream, "\xEF\xBB\xBF");
58
+ fputcsv($stream, ["ID"]);
59
+
60
+ // 各行を書き込む
61
+ foreach ($stmt as $row) {
62
+ fputcsv($stream, [$row['id']]);
63
+ }
64
+ ```

1

追記

2019/01/28 20:25

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -5,4 +5,4 @@
5
5
  `$stream = fopen('php://output', 'wb');`
6
6
 
7
7
  - `php://temp` はテンポラリメモリ(書き込んだあと読み出さないなら何の意味もない)
8
- - `php://output` は mod_php や php-fpm におけるHTTPのボディパート出力
8
+ - `php://output` は mod_php や php-fpm におけるHTTPのボディパート出力(`echo` で出すのと同じ場所)