質問編集履歴

1

ソースの追加

2018/10/29 13:26

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -18,6 +18,430 @@
18
18
 
19
19
 
20
20
 
21
-
21
+ ```php
22
+
22
-
23
+ <?php
24
+
25
+ define( "FILE_DIR", "images/test/");
26
+
27
+ // 変数の初期化
28
+
29
+ $page_flag = 0;
30
+
31
+ $clean = array();
32
+
33
+ $error = array();
34
+
35
+ // サニタイズ
36
+
37
+ if( !empty($_POST) ) {
38
+
39
+ foreach( $_POST as $key => $value ) {
40
+
41
+ $clean[$key] = htmlspecialchars( $value, ENT_QUOTES);
42
+
43
+ }
44
+
45
+ }
46
+
47
+ if( !empty($clean['btn_confirm']) ) {
48
+
49
+ $error = validation($clean);
50
+
51
+ // ファイルのアップロード
52
+
23
- [ソースはこちらを参考にしております。](https://github.com/gray-code/make-the-form/blob/master/vol8/index.php)
53
+ if( !empty($_FILES['attachment_file']['tmp_name']) or !empty($_FILES['attachment_file02']['tmp_name']) ) {
54
+
55
+ $upload_res = move_uploaded_file( $_FILES['attachment_file']['tmp_name'], FILE_DIR.$_FILES['attachment_file']['name']);
56
+
57
+
58
+
59
+ $upload_res02 = move_uploaded_file( $_FILES['attachment_file02']['tmp_name'], FILE_DIR.$_FILES['attachment_file02']['name']);
60
+
61
+ if( $upload_res !== true or $upload_res02 !== true ) {
62
+
63
+ $error[] = 'ファイルのアップロードに失敗しました。';
64
+
65
+ } else {
66
+
67
+ $clean['attachment_file'] = $_FILES['attachment_file']['name'];
68
+
69
+ }
70
+
71
+ }
72
+
73
+ if( empty($error) ) {
74
+
75
+ $page_flag = 1;
76
+
77
+ // セッションの書き込み
78
+
79
+ session_start();
80
+
81
+ $_SESSION['page'] = true;
82
+
83
+ }
84
+
85
+ } elseif( !empty($clean['btn_submit']) ) {
86
+
87
+ session_start();
88
+
89
+ if( !empty($_SESSION['page']) && $_SESSION['page'] === true ) {
90
+
91
+ // セッションの削除
92
+
93
+ unset($_SESSION['page']);
94
+
95
+ $page_flag = 2;
96
+
97
+ // 変数とタイムゾーンを初期化
98
+
99
+ $header = null;
100
+
101
+ $body = null;
102
+
103
+ $admin_body = null;
104
+
105
+ $auto_reply_subject = null;
106
+
107
+ $auto_reply_text = null;
108
+
109
+ $admin_reply_subject = null;
110
+
111
+ $admin_reply_text = null;
112
+
113
+ date_default_timezone_set('Asia/Tokyo');
114
+
115
+
116
+
117
+ //日本語の使用宣言
118
+
119
+ mb_language("ja");
120
+
121
+ mb_internal_encoding("UTF-8");
122
+
123
+
124
+
125
+ $header = "MIME-Version: 1.0\n";
126
+
127
+ $header = "Content-Type: multipart/mixed;boundary=\"__BOUNDARY__\"\n";
128
+
129
+ $header .= "From: GRAYCODE <yytsm100@yahoo.co,jp>\n";
130
+
131
+ $header .= "Reply-To: GRAYCODE <yytsm100@yahoo.co,jp>\n";
132
+
133
+
134
+
135
+ // 件名を設定
136
+
137
+ $auto_reply_subject = 'お問い合わせありがとうございます。';
138
+
139
+
140
+
141
+ // 本文を設定
142
+
143
+ $auto_reply_text = "この度は、お問い合わせ頂き誠にありがとうございます。
144
+
145
+ 下記の内容でお問い合わせを受け付けました。\n\n";
146
+
147
+ $auto_reply_text .= "お問い合わせ日時:" . date("Y-m-d H:i") . "\n";
148
+
149
+ $auto_reply_text .= "お問い合わせ内容:" . nl2br($clean['contact']) . "\n\n";
150
+
151
+ $auto_reply_text .= "test";
152
+
153
+
154
+
155
+ // テキストメッセージをセット
156
+
157
+ $body = "--__BOUNDARY__\n";
158
+
159
+ $body .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n\n";
160
+
161
+ $body .= $auto_reply_text . "\n";
162
+
163
+ $body .= "--__BOUNDARY__\n";
164
+
165
+
166
+
167
+ // ファイルを添付
168
+
169
+ if( !empty($clean['attachment_file']) ) {
170
+
171
+ $body .= "Content-Type: application/octet-stream; name=\"{$clean['attachment_file']}\"\n";
172
+
173
+ $body .= "Content-Disposition: attachment; filename=\"{$clean['attachment_file']}\"\n";
174
+
175
+ $body .= "Content-Transfer-Encoding: base64\n";
176
+
177
+ $body .= "\n";
178
+
179
+ $body .= chunk_split(base64_encode(file_get_contents(FILE_DIR.$clean['attachment_file'])));
180
+
181
+ $body .= "--__BOUNDARY__\n";
182
+
183
+ } // ファイルを添付
184
+
185
+ if( !empty($clean['attachment_file02']) ) {
186
+
187
+ $body .= "Content-Type: application/octet-stream; name=\"{$clean['attachment_file02']}\"\n";
188
+
189
+ $body .= "Content-Disposition: attachment; filename=\"{$clean['attachment_file02']}\"\n";
190
+
191
+ $body .= "Content-Transfer-Encoding: base64\n";
192
+
193
+ $body .= "\n";
194
+
195
+ $body .= chunk_split(base64_encode(file_get_contents(FILE_DIR.$clean['attachment_file02'])));
196
+
197
+ $body .= "--__BOUNDARY__\n";
198
+
199
+ }
200
+
201
+
202
+
203
+ // 自動返信メール送信
204
+
205
+ mb_send_mail( $clean['email'], $auto_reply_subject, $body, $header);
206
+
207
+
208
+
209
+ // 運営側へ送るメールの件名
210
+
211
+ $admin_reply_subject = "お問い合わせを受け付けました";
212
+
213
+
214
+
215
+ // 本文を設定
216
+
217
+ $admin_reply_text = "下記の内容でお問い合わせがありました。\n\n";
218
+
219
+ $admin_reply_text .= "お問い合わせ日時:" . date("Y-m-d H:i") . "\n";
220
+
221
+
222
+
223
+ $admin_reply_text .= "お問い合わせ内容:" . nl2br($clean['contact']) . "\n\n";
224
+
225
+
226
+
227
+ // テキストメッセージをセット
228
+
229
+ $body = "--__BOUNDARY__\n";
230
+
231
+ $body .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n\n";
232
+
233
+ $body .= $admin_reply_text . "\n";
234
+
235
+ $body .= "--__BOUNDARY__\n";
236
+
237
+
238
+
239
+ // ファイルを添付
240
+
241
+ if( !empty($clean['attachment_file']) ) {
242
+
243
+ $body .= "Content-Type: application/octet-stream; name=\"{$clean['attachment_file']}\"\n";
244
+
245
+ $body .= "Content-Disposition: attachment; filename=\"{$clean['attachment_file']}\"\n";
246
+
247
+ $body .= "Content-Transfer-Encoding: base64\n";
248
+
249
+ $body .= "\n";
250
+
251
+ $body .= chunk_split(base64_encode(file_get_contents(FILE_DIR.$clean['attachment_file'])));
252
+
253
+ $body .= "--__BOUNDARY__\n";
254
+
255
+ }
256
+
257
+ // ファイルを添付
258
+
259
+ if( !empty($clean['attachment_file02']) ) {
260
+
261
+ $body .= "Content-Type: application/octet-stream; name=\"{$clean['attachment_file02']}\"\n";
262
+
263
+ $body .= "Content-Disposition: attachment; filename=\"{$clean['attachment_file02']}\"\n";
264
+
265
+ $body .= "Content-Transfer-Encoding: base64\n";
266
+
267
+ $body .= "\n";
268
+
269
+ $body .= chunk_split(base64_encode(file_get_contents(FILE_DIR.$clean['attachment_file02'])));
270
+
271
+ $body .= "--__BOUNDARY__\n";
272
+
273
+ }
274
+
275
+
276
+
277
+ // 管理者へメール送信
278
+
279
+ mb_send_mail( 'yytsm100@yahoo.co,jp', $admin_reply_subject, $body, $header);
280
+
281
+
282
+
283
+ } else {
284
+
285
+ $page_flag = 0;
286
+
287
+ }
288
+
289
+ }
290
+
291
+ function validation($data) {
292
+
293
+ $error = array();
294
+
295
+
296
+
297
+ // お問い合わせ内容のバリデーション
298
+
299
+ if( empty($data['contact']) ) {
300
+
301
+ $error[] = "「お問い合わせ内容」は必ず入力してください。";
302
+
303
+ }
304
+
305
+
306
+
307
+ return $error;
308
+
309
+ }
310
+
311
+ ?>
312
+
313
+
314
+
315
+ <!DOCTYPE>
316
+
317
+ <html lang="ja">
318
+
319
+ <head>
320
+
321
+ <title>お問い合わせフォーム</title>
322
+
323
+
324
+
325
+ </head>
326
+
327
+ <body>
328
+
329
+ <h1>お問い合わせフォーム</h1>
330
+
331
+ <?php if( $page_flag === 1 ): ?>
332
+
333
+
334
+
335
+ <form method="post" action="">
336
+
337
+
338
+
339
+ <div class="element_wrap">
340
+
341
+ <label>お問い合わせ内容</label>
342
+
343
+ <p><?php echo nl2br($clean['contact']); ?></p>
344
+
345
+ </div>
346
+
347
+ <div class="element_wrap">
348
+
349
+ <label>画像ファイルの添付</label>
350
+
351
+ <?php if( !empty($clean['attachment_file']) or empty($clean['attachment_file02']) ): ?>
352
+
353
+ <p><img src="<?php echo FILE_DIR.$clean['attachment_file']; ?>"></p>
354
+
355
+ <p><img src="<?php echo FILE_DIR.$clean['attachment_file02']; ?>"></p>
356
+
357
+ <?php endif; ?>
358
+
359
+ </div>
360
+
361
+ <input type="submit" name="btn_back" value="戻る">
362
+
363
+ <input type="submit" name="btn_submit" value="送信">
364
+
365
+
366
+
367
+ <input type="hidden" name="contact" value="<?php echo $clean['contact']; ?>">
368
+
369
+ <?php if( !empty($clean['attachment_file']) or !empty($clean['attachment_file02'])): ?>
370
+
371
+ <input type="hidden" name="attachment_file" value="<?php echo $clean['attachment_file']; ?>">
372
+
373
+ <input type="hidden" name="attachment_file02" value="<?php echo $clean['attachment_file02']; ?>">
374
+
375
+ <?php endif; ?>
376
+
377
+ </form>
378
+
379
+
380
+
381
+ <?php elseif( $page_flag === 2 ): ?>
382
+
383
+
384
+
385
+ <p>送信が完了しました。</p>
386
+
387
+
388
+
389
+ <?php else: ?>
390
+
391
+
392
+
393
+ <?php if( !empty($error) ): ?>
394
+
395
+ <ul class="error_list">
396
+
397
+ <?php foreach( $error as $value ): ?>
398
+
399
+ <li><?php echo $value; ?></li>
400
+
401
+ <?php endforeach; ?>
402
+
403
+ </ul>
404
+
405
+ <?php endif; ?>
406
+
407
+
408
+
409
+ <form method="post" action="" enctype="multipart/form-data">
410
+
411
+
412
+
413
+
414
+
415
+ <div class="element_wrap">
416
+
417
+ <label>お問い合わせ内容</label>
418
+
419
+ <textarea name="contact"><?php if( !empty($clean['contact']) ){ echo $clean['contact']; } ?></textarea>
420
+
421
+ </div>
422
+
423
+ <div class="element_wrap">
424
+
425
+ <label>画像ファイルの添付</label>
426
+
427
+ <input type="file" name="attachment_file" multiple>
428
+
429
+
430
+
431
+ <input type="file" name="attachment_file02" multiple>
432
+
433
+ </div>
434
+
435
+ <input type="submit" name="btn_confirm" value="入力内容を確認する">
436
+
437
+ </form>
438
+
439
+
440
+
441
+ <?php endif; ?>
442
+
443
+ </body>
444
+
445
+ </htm>
446
+
447
+ ```