Webform - CSV ダウンロードファイルの文字化け修正
Webform モジュールのダウンロード機能を使って CSV ファイルのダウンロードを行った場合、ブラウザやアプリケーションの種類によっては文字化けを起こすという問題の修正。
修正というよりは無理やりどうにかしてる…と言った方がよさそうだけど、まぁ。
コード改変メモ
- _webform_results_download() 関数内で、ファイルの書き込み時に文字コードの変換処理を行うよう変更。
- CSV ファイル内の項目名などを翻訳対象文字列に変更。
Index: webform_report.inc
===================================================================
--- webform_report.inc,v 1.15.2.11 2008/01/26 00:54:06 quicksketch
+++ webform_report.inc (patch)
@@ -197,8 +197,9 @@
$file_name = tempnam(variable_get('file_directory_temp', FILE_DIRECTORY_TEMP), 'webform');
$handle = @fopen($file_name, 'w'); // The @ suppresses errors.
$header[0] .= $node->title .",,,,";
- $header[1] .= "Submission Details,,,,,";
- $header[2] .= "Serial,SID,Time,IP Address,UID,Username";
+ // ヘッダ部分を翻訳対象文字列に変更
+ $header[1] .= t('Submission Details') . ",,,,,";
+ $header[2] .= t('Serial') . "," . t('SID') . "," . t('Time') . "," . t('IP Address') . "," . t('UID') . "," . t('Username');
// Compile header information.
_webform_load_components(); // Load all components.
foreach ($node->webformcomponents as $cid => $component) {
@@ -213,8 +214,9 @@
}
// Write header information.
+ // ファイル書込み時にヘッダ部分の文字コードを変換
$file_record = $header[0] ."\n". $header[1] ."\n". $header[2] ."\n";
- @fwrite($handle, $file_record);
+ @fwrite($handle, mb_convert_encoding($file_record, "Shift-JIS", "UTF-8"));
// Get all the submissions for the node.
$submissions = _webform_fetch_submissions($nid);
@@ -231,13 +233,15 @@
}
}
// Write data from submissions.
- @fwrite($handle, $row ."\n");
+ // ファイル書込み時にフォーム入力内容の文字コードを変換
+ @fwrite($handle, mb_convert_encoding($row, "Shift-JIS", "UTF-8") ."\n");
}
// Close the file.
@fclose($handle);
drupal_set_header("Content-type: text/csv; charset=utf-8");
- drupal_set_header("Content-Disposition: attachment; filename=". preg_replace('/\.$/', '', str_replace(' ', '_', $node->title)) .".csv");
+ // ファイル名の文字コードを変換
+ drupal_set_header("Content-Disposition: attachment; filename=". preg_replace('/\.$/', '', str_replace(' ', '_', mb_convert_encoding($node->title, "Shift-JIS", "UTF-8"))) .".csv");
@readfile($file_name); // The @ makes it silent.
@unlink($file_name); // Clean up, the @ makes it silent.
===================================================================
--- webform_report.inc,v 1.15.2.11 2008/01/26 00:54:06 quicksketch
+++ webform_report.inc (patch)
@@ -197,8 +197,9 @@
$file_name = tempnam(variable_get('file_directory_temp', FILE_DIRECTORY_TEMP), 'webform');
$handle = @fopen($file_name, 'w'); // The @ suppresses errors.
$header[0] .= $node->title .",,,,";
- $header[1] .= "Submission Details,,,,,";
- $header[2] .= "Serial,SID,Time,IP Address,UID,Username";
+ // ヘッダ部分を翻訳対象文字列に変更
+ $header[1] .= t('Submission Details') . ",,,,,";
+ $header[2] .= t('Serial') . "," . t('SID') . "," . t('Time') . "," . t('IP Address') . "," . t('UID') . "," . t('Username');
// Compile header information.
_webform_load_components(); // Load all components.
foreach ($node->webformcomponents as $cid => $component) {
@@ -213,8 +214,9 @@
}
// Write header information.
+ // ファイル書込み時にヘッダ部分の文字コードを変換
$file_record = $header[0] ."\n". $header[1] ."\n". $header[2] ."\n";
- @fwrite($handle, $file_record);
+ @fwrite($handle, mb_convert_encoding($file_record, "Shift-JIS", "UTF-8"));
// Get all the submissions for the node.
$submissions = _webform_fetch_submissions($nid);
@@ -231,13 +233,15 @@
}
}
// Write data from submissions.
- @fwrite($handle, $row ."\n");
+ // ファイル書込み時にフォーム入力内容の文字コードを変換
+ @fwrite($handle, mb_convert_encoding($row, "Shift-JIS", "UTF-8") ."\n");
}
// Close the file.
@fclose($handle);
drupal_set_header("Content-type: text/csv; charset=utf-8");
- drupal_set_header("Content-Disposition: attachment; filename=". preg_replace('/\.$/', '', str_replace(' ', '_', $node->title)) .".csv");
+ // ファイル名の文字コードを変換
+ drupal_set_header("Content-Disposition: attachment; filename=". preg_replace('/\.$/', '', str_replace(' ', '_', mb_convert_encoding($node->title, "Shift-JIS", "UTF-8"))) .".csv");
@readfile($file_name); // The @ makes it silent.
@unlink($file_name); // Clean up, the @ makes it silent.
UTF-8 では使えるけど Shift-JIS では使えないという文字列があった場合は、逆に文字化けしてしまうと思うので、あくまでも日本語前提ということで…


最近のコメント
6日 8時間前
6日 8時間前
6日 10時間前
6日 16時間前
6日 20時間前
6日 21時間前
6日 23時間前