php にてCSVファイルアップロード→CSVの内容を確認画面で表示→MySQLへインポートというものを作りたいと思い、下記を参考に作成してみました。
■問題点
・確認画面でCSVファイルの内容は表示されるものの、MySQLへデータが挿入されません。
・確認画面/完了ページの切り替えをhiddenのmodeを使ってやろうとしているのですが、それが間違いなのでしょうか。
(確認画面の表示処理とDBへの登録処理がごちゃごちゃになってきてしまい、非常にわかりにくくなってきてしまいました。)
php
1<?php 2 3$mode = (string) filter_input ( INPUT_POST, 'mode' ); 4 5if ( isset($_FILES['csvfile']['error']) && is_int($_FILES['csvfile']['error']) ) { 6 7 try { 8 9 // エラーチェック 10 switch ($_FILES['csvfile']['error']) { 11 case UPLOAD_ERR_OK: 12 // エラーなし 13 break; 14 case UPLOAD_ERR_NO_FILE: 15 //ファイル未選択 16 throw new RuntimeException ('File is not selected'); 17 case UPLOAD_ERR_INI_SIZE: 18 case UPLOAD_ERR_FORM_SIZE: 19 // 許可サイズを超過 20 throw new RUntimeException ('File is too large'); 21 default: 22 throw new RUntimeException ('Unknown error'); 23 } 24 25 // 一時ファイル名 26 $tmp_name = $_FILES['csvfile']['tmp_name']; 27 28 $detect_order = 'ASCII,JIS,UTF-8,CPS51932,SJIS-win'; 29 setlocale(LC_ALL, 'ja_JP.UTF-8'); 30 31 // 文字コードを変換 32 $buffer = file_get_contents($tmp_name); 33 34 if ( !$encoding = mb_detect_encoding( $buffer, $detect_order, true ) ) { 35 // 文字コードの自動判定に失敗 36 unset ($buffer); 37 throw new RuntimeException('Character set detection failed'); 38 } 39 40 file_put_contents ( $tmp_name, mb_convert_encoding($buffer, 'UTF-8', $encoding) ); 41 unset ($buffer); 42 43 44 $dsn = "mysql:dbname=guestbook;host=localhost;charset=utf8"; 45 $pdo = new PDO ( $dsn, 'test', 'test' ); 46 $pdo->setAttribute ( PDO::ATTR_EMULATE_PREPARES, false ); 47 $pdo->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 48 $sql = "INSERT INTO ms_member (username, password, reg_date) VALUES (?, ?, NOW())"; 49 $stmt = $pdo->prepare($sql); 50 51 // トランザクション処理 52 $pdo->beginTransaction(); 53 54 try { 55 $fp = fopen( $tmp_name, 'rb' ); 56 //var_dump($tmp); 57 58 // 確認用テーブル表示 59 if ( $mode === "confirm" ) { 60 echo "<table border=\"1\">\n"; 61 while ( $rows = fgetcsv( $fp, "1024") ) { 62 echo "<tr>\n<td>{$rows[0]}</td>\n"; 63 echo "<td>{$rows[1]}</td>\n"; 64 echo "<td>{$rows[2]}</td>\n"; 65 echo "<td>{$rows[3]}</td>\n</tr>\n"; 66 } 67 echo "</table>\n"; 68 69 // 完了 70 } else if ( $mode === "finish" ) { 71 while ( $rows = fgetcsv( $fp ) ) { 72 if ( $row === array(null) ){ 73 // 空行はスキップ 74 continue; 75 } 76 77 $stmt->bindValue (1, $row[0], PDO::PARAM_STR); 78 $stmt->bindValue (2, $row[1], PDO::PARAM_STR); 79 $stmt->bindValue (3, $row[2], PDO::PARAM_INT); 80 81 $executed = $stmt->execute($row); 82 } 83 if ( !feof($fp) ) { 84 // ファイルポインタが終端に達していなければエラー 85 throw new RuntimeException ( 'CSV parsing error' ); 86 } 87 fclose($fp); 88 $pdo->commit(); 89 } 90 91 } catch (Exception $e) { 92 93 if ( $mode === "finish" ) { 94 fclose($fp); 95 $pdo->rollBack(); 96 throw $e; 97 } 98 99 } 100 101 if ( $mode === "finish" ) { 102 if ( isset($executed) ) { 103 $err_msg = 'Import Successful'; 104 header ( 'Location: ./finish.html' ); 105 } else { 106 $err_msg = 'There was nothing to import'; 107 } 108 } 109 110 } catch (Exception $e) { 111 $err_msg = $e->getMessage(); 112 } 113} 114 115header ( 'Content-Type: text/html; charset=utf-8' ); 116?> 117<!DOCTYPE html> 118<html lang="ja"> 119<head> 120 <title>csvアップロード</title> 121</head> 122<body> 123 124 <?php if (isset($err_msg)) { 125 echo "<p style=\"color:red;\">" . $err_msg . "</p>"; 126 }?> 127 128 <?php 129 // POSTされていない状態 130 if ( !$mode ) { 131 echo <<<EOD 132 <form action="" method="post" enctype="multipart/form-data"> 133 <input type="file" name="csvfile" size="30"><br> 134 <input type="submit" value="アップロード確認"> 135 <input type="hidden" name="mode" value="confirm"> 136 </form> 137EOD; 138 139 // 確認画面 140 } else if ( $mode === 'confirm' ) { 141 echo <<<EOD 142 <form action="" method="post"> 143 <input type="submit" value="アップロード完了"> 144 <input type="hidden" name="mode" value="finish"> 145 </form> 146EOD; 147 } 148 ?> 149 150</body> 151</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/05 10:17