こちらのサイトを参考に
フォームに画像をアップロードさせる機能を追加しようとしています。
画像のアップロードのinputタグを複数設置したいと考えているのですが、
エラーとなり、上手くいきません。
attachment_fileをattachment_file02と追加し、
フォームを複数設置しようと考えたのですが、
エラーが起きてしまいました。
php
1<?php 2define( "FILE_DIR", "images/test/"); 3// 変数の初期化 4$page_flag = 0; 5$clean = array(); 6$error = array(); 7// サニタイズ 8if( !empty($_POST) ) { 9 foreach( $_POST as $key => $value ) { 10 $clean[$key] = htmlspecialchars( $value, ENT_QUOTES); 11 } 12} 13if( !empty($clean['btn_confirm']) ) { 14 $error = validation($clean); 15 // ファイルのアップロード 16 if( !empty($_FILES['attachment_file']['tmp_name']) or !empty($_FILES['attachment_file02']['tmp_name']) ) { 17 $upload_res = move_uploaded_file( $_FILES['attachment_file']['tmp_name'], FILE_DIR.$_FILES['attachment_file']['name']); 18 19 $upload_res02 = move_uploaded_file( $_FILES['attachment_file02']['tmp_name'], FILE_DIR.$_FILES['attachment_file02']['name']); 20 if( $upload_res !== true or $upload_res02 !== true ) { 21 $error[] = 'ファイルのアップロードに失敗しました。'; 22 } else { 23 $clean['attachment_file'] = $_FILES['attachment_file']['name']; 24 } 25 } 26 if( empty($error) ) { 27 $page_flag = 1; 28 // セッションの書き込み 29 session_start(); 30 $_SESSION['page'] = true; 31 } 32} elseif( !empty($clean['btn_submit']) ) { 33 session_start(); 34 if( !empty($_SESSION['page']) && $_SESSION['page'] === true ) { 35 // セッションの削除 36 unset($_SESSION['page']); 37 $page_flag = 2; 38 // 変数とタイムゾーンを初期化 39 $header = null; 40 $body = null; 41 $admin_body = null; 42 $auto_reply_subject = null; 43 $auto_reply_text = null; 44 $admin_reply_subject = null; 45 $admin_reply_text = null; 46 date_default_timezone_set('Asia/Tokyo'); 47 48 //日本語の使用宣言 49 mb_language("ja"); 50 mb_internal_encoding("UTF-8"); 51 52 $header = "MIME-Version: 1.0\n"; 53 $header = "Content-Type: multipart/mixed;boundary=\"__BOUNDARY__\"\n"; 54 $header .= "From: GRAYCODE <yytsm100@yahoo.co,jp>\n"; 55 $header .= "Reply-To: GRAYCODE <yytsm100@yahoo.co,jp>\n"; 56 57 // 件名を設定 58 $auto_reply_subject = 'お問い合わせありがとうございます。'; 59 60 // 本文を設定 61 $auto_reply_text = "この度は、お問い合わせ頂き誠にありがとうございます。 62 下記の内容でお問い合わせを受け付けました。\n\n"; 63 $auto_reply_text .= "お問い合わせ日時:" . date("Y-m-d H:i") . "\n"; 64 $auto_reply_text .= "お問い合わせ内容:" . nl2br($clean['contact']) . "\n\n"; 65 $auto_reply_text .= "test"; 66 67 // テキストメッセージをセット 68 $body = "--__BOUNDARY__\n"; 69 $body .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n\n"; 70 $body .= $auto_reply_text . "\n"; 71 $body .= "--__BOUNDARY__\n"; 72 73 // ファイルを添付 74 if( !empty($clean['attachment_file']) ) { 75 $body .= "Content-Type: application/octet-stream; name=\"{$clean['attachment_file']}\"\n"; 76 $body .= "Content-Disposition: attachment; filename=\"{$clean['attachment_file']}\"\n"; 77 $body .= "Content-Transfer-Encoding: base64\n"; 78 $body .= "\n"; 79 $body .= chunk_split(base64_encode(file_get_contents(FILE_DIR.$clean['attachment_file']))); 80 $body .= "--__BOUNDARY__\n"; 81 } // ファイルを添付 82 if( !empty($clean['attachment_file02']) ) { 83 $body .= "Content-Type: application/octet-stream; name=\"{$clean['attachment_file02']}\"\n"; 84 $body .= "Content-Disposition: attachment; filename=\"{$clean['attachment_file02']}\"\n"; 85 $body .= "Content-Transfer-Encoding: base64\n"; 86 $body .= "\n"; 87 $body .= chunk_split(base64_encode(file_get_contents(FILE_DIR.$clean['attachment_file02']))); 88 $body .= "--__BOUNDARY__\n"; 89 } 90 91 // 自動返信メール送信 92 mb_send_mail( $clean['email'], $auto_reply_subject, $body, $header); 93 94 // 運営側へ送るメールの件名 95 $admin_reply_subject = "お問い合わせを受け付けました"; 96 97 // 本文を設定 98 $admin_reply_text = "下記の内容でお問い合わせがありました。\n\n"; 99 $admin_reply_text .= "お問い合わせ日時:" . date("Y-m-d H:i") . "\n"; 100 101 $admin_reply_text .= "お問い合わせ内容:" . nl2br($clean['contact']) . "\n\n"; 102 103 // テキストメッセージをセット 104 $body = "--__BOUNDARY__\n"; 105 $body .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n\n"; 106 $body .= $admin_reply_text . "\n"; 107 $body .= "--__BOUNDARY__\n"; 108 109 // ファイルを添付 110 if( !empty($clean['attachment_file']) ) { 111 $body .= "Content-Type: application/octet-stream; name=\"{$clean['attachment_file']}\"\n"; 112 $body .= "Content-Disposition: attachment; filename=\"{$clean['attachment_file']}\"\n"; 113 $body .= "Content-Transfer-Encoding: base64\n"; 114 $body .= "\n"; 115 $body .= chunk_split(base64_encode(file_get_contents(FILE_DIR.$clean['attachment_file']))); 116 $body .= "--__BOUNDARY__\n"; 117 } 118 // ファイルを添付 119 if( !empty($clean['attachment_file02']) ) { 120 $body .= "Content-Type: application/octet-stream; name=\"{$clean['attachment_file02']}\"\n"; 121 $body .= "Content-Disposition: attachment; filename=\"{$clean['attachment_file02']}\"\n"; 122 $body .= "Content-Transfer-Encoding: base64\n"; 123 $body .= "\n"; 124 $body .= chunk_split(base64_encode(file_get_contents(FILE_DIR.$clean['attachment_file02']))); 125 $body .= "--__BOUNDARY__\n"; 126 } 127 128 // 管理者へメール送信 129 mb_send_mail( 'yytsm100@yahoo.co,jp', $admin_reply_subject, $body, $header); 130 131 } else { 132 $page_flag = 0; 133 } 134} 135function validation($data) { 136 $error = array(); 137 138 // お問い合わせ内容のバリデーション 139 if( empty($data['contact']) ) { 140 $error[] = "「お問い合わせ内容」は必ず入力してください。"; 141 } 142 143 return $error; 144} 145?> 146 147<!DOCTYPE> 148<html lang="ja"> 149<head> 150<title>お問い合わせフォーム</title> 151 152</head> 153<body> 154<h1>お問い合わせフォーム</h1> 155<?php if( $page_flag === 1 ): ?> 156 157<form method="post" action=""> 158 159 <div class="element_wrap"> 160 <label>お問い合わせ内容</label> 161 <p><?php echo nl2br($clean['contact']); ?></p> 162 </div> 163 <div class="element_wrap"> 164 <label>画像ファイルの添付</label> 165 <?php if( !empty($clean['attachment_file']) or empty($clean['attachment_file02']) ): ?> 166 <p><img src="<?php echo FILE_DIR.$clean['attachment_file']; ?>"></p> 167 <p><img src="<?php echo FILE_DIR.$clean['attachment_file02']; ?>"></p> 168 <?php endif; ?> 169 </div> 170<input type="submit" name="btn_back" value="戻る"> 171 <input type="submit" name="btn_submit" value="送信"> 172 173 <input type="hidden" name="contact" value="<?php echo $clean['contact']; ?>"> 174 <?php if( !empty($clean['attachment_file']) or !empty($clean['attachment_file02'])): ?> 175 <input type="hidden" name="attachment_file" value="<?php echo $clean['attachment_file']; ?>"> 176 <input type="hidden" name="attachment_file02" value="<?php echo $clean['attachment_file02']; ?>"> 177 <?php endif; ?> 178</form> 179 180<?php elseif( $page_flag === 2 ): ?> 181 182<p>送信が完了しました。</p> 183 184<?php else: ?> 185 186<?php if( !empty($error) ): ?> 187 <ul class="error_list"> 188 <?php foreach( $error as $value ): ?> 189 <li><?php echo $value; ?></li> 190 <?php endforeach; ?> 191 </ul> 192<?php endif; ?> 193 194<form method="post" action="" enctype="multipart/form-data"> 195 196 197 <div class="element_wrap"> 198 <label>お問い合わせ内容</label> 199 <textarea name="contact"><?php if( !empty($clean['contact']) ){ echo $clean['contact']; } ?></textarea> 200 </div> 201 <div class="element_wrap"> 202 <label>画像ファイルの添付</label> 203 <input type="file" name="attachment_file" multiple> 204 205 <input type="file" name="attachment_file02" multiple> 206 </div> 207 <input type="submit" name="btn_confirm" value="入力内容を確認する"> 208</form> 209 210<?php endif; ?> 211</body> 212</htm>
エラー文とご自身でお書きになったコードをご提示ください。参考記事が間違っていない限りどこがおかしいか指摘するのは困難です。
エラーメッセージがどこにどう表示されているのか、指摘された行の前後10行くらいは掲示してくれないとなんとも言えなくない?
参考にしたものと実際質問者さんが組んだコードは別物ですよ。ご自身が組んだコードを提示してください
回答2件
あなたの回答
tips
プレビュー