Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Jak przerobić ten skrypt formularzu kontaktowego?
Forum PHP.pl > Forum > Przedszkole
Mistiqe
Witam,

mam pewien problem ze skryptem formularzu kontaktowego. Otóż mam ten skrypt:

  1. <?php
  2. include_once('config.php');
  3. include_once('functions.php');
  4.  
  5. //If contact is being sent:
  6. if($_POST['submit_id'] == 1){
  7. //Check name entered
  8. if($_POST['name'] == NULL){ $message = 'Please enter your name.';}
  9.  
  10. //check if email is enetered
  11. if($message == NULL && is_valid_email($_POST['email']) == false ){ $message = 'Please enter a valid email.';}
  12.  
  13. //check if message is entered
  14. if($_POST['message_text'] == NULL && $message == NULL){ $message = 'Please enter a comment.';}
  15.  
  16. //File Upload checks
  17. if($message == NULL && $FILE_UPLOAD == 1 && $_FILES['user_file']['name'] != NULL){
  18. if($_FILES['user_file']['size'] > (($FILE_UPLOAD_MAX*1024)*1024)){ $message = 'File is over '.$FILE_UPLOAD_MAX.' MB in size.';}
  19. if($message == NULL && allowed_ext($FILE_UPLOADS_EXT,$_FILES['user_file']['name']) == false){$message = 'Invalid extension.';}
  20. $new_filename = date("G_i_s_").$_FILES['user_file']['name'];
  21. }
  22.  
  23. //Image verificaiton checks
  24. if($message == NULL && $IMAGE_VERIFICATION == 1){
  25. $te_co = hex2bin($_POST['hid_code']);
  26. $word_is = RC4($te_co,$IMAGE_VER_CODE);
  27. if($word_is != $_POST['confirm_image']){$message = 'Your verfication code is incorrect.';}
  28. }
  29. //End verifications, start processing
  30. if($message == NULL){
  31. //Check if file upload is needed
  32. if($FILE_UPLOAD == 1 && $_FILES['user_file']['name'] != NULL){
  33. //Store file for keep and email
  34. move_uploaded_file($_FILES['user_file']['tmp_name'],$FILE_UPLOADS_DIR.$new_filename);
  35. }
  36. //compose admin/user message templates replaces
  37. $do_search = array('$+name+$','$+email+$','$+message_text+$','$+reason+$');
  38. $do_replace = array($_POST['name'],$_POST['email'],$_POST['message_text'],$_POST['reason']);
  39.  
  40.  
  41. //Send user email?
  42. if($SEND_THANKS == 1){
  43. $user_message = str_replace($do_search,$do_replace,$USER_TEMPLATE);
  44. //Set Headers
  45. $user_header = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
  46. $user_header .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
  47. $user_header .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n";
  48. //Send Thank you
  49. mail ($_POST['email'],$EMAIL_OPTIONS['USER_SUBJECT'],$user_message,$user_header);
  50. }
  51.  
  52. //Send admi email?
  53. if(count($ADMIN_EMAILS) > 0){
  54. $admin_message = str_replace($do_search,$do_replace,$ADMIN_TEMPLATE);
  55. //Do we need to send file as attachment?
  56. if($FILE_DO != 1){
  57. //Get file attriubtes
  58. $fileatt_type = $_FILES['user_file']['type'];
  59.  
  60. $file = fopen($FILE_UPLOADS_DIR.$new_filename,'rb');
  61. while($dat = fread($file,1025657)){
  62. $attachment_data .= $dat;
  63. }
  64. fclose($file);
  65.  
  66. // Encode file content
  67. $attachment_data = chunk_split(base64_encode($attachment_data));
  68. //File upload headers
  69. $semi_rand = md5(time());
  70. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  71.  
  72. $headers = "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">";
  73.  
  74. // Add the headers for a file attachment
  75. $headers .= "\nMIME-Version: 1.0\n" .
  76. "Content-Type: multipart/mixed;\n" .
  77. " boundary=\"{$mime_boundary}\"";
  78.  
  79. // Add a multipart boundary above the plain message
  80. $new_message = "This is a multi-part message in MIME format.\n\n" .
  81. "--{$mime_boundary}\n" .
  82. "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=\"".$EMAIL_OPTIONS['CHARSET']."\"\n" .
  83. "Content-Transfer-Encoding: 7bit\n\n" .
  84. $admin_message . "\n\n";
  85.  
  86. // Add file attachment to the message
  87. $new_message .= "--{$mime_boundary}\n" .
  88. "Content-Type: {$fileatt_type};\n" .
  89. " name=\"{$new_filename}\"\n" .
  90. "Content-Disposition: attachment;\n" .
  91. " filename=\"{$new_filename}\"\n" .
  92. "Content-Transfer-Encoding: base64\n\n" .
  93. $attachment_data . "\n\n" .
  94. "--{$mime_boundary}--\n";
  95.  
  96. unset($attachment_data);
  97. } else {
  98. //regular headers
  99. $headers = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
  100. $headers .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
  101. $headers .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n";
  102. $new_message = $admin_message;
  103. }
  104. //Send admin emails
  105. foreach($ADMIN_EMAILS as $this_email){
  106. mail ($this_email,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
  107. }
  108. }
  109.  
  110. //Remove file if not needed
  111. if($FILE_DO == 2){
  112. unlink($FILE_UPLOADS_DIR.$new_filename);
  113. }
  114. $message = 'Your contact has been sent, thank you.';
  115. $_POST = NULL;
  116. }
  117. }
  118. if($message != NULL){
  119. ?>
  120. <table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#FF8080">
  121. <tr>
  122. <td bgcolor="#FFD5D5"><font color="#FF0000"><?=$message;?></font></td>
  123. </tr>
  124. </table>
  125. <br/>
  126. <?php } ?>
  127. <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data" name="contact" id="contact" style="display:inline;">
  128. <table width="100%" border="0" align="left" cellpadding="5" cellspacing="0">
  129. <tr>
  130. <td>Name:</td>
  131. <td><input name="name" type="text" id="name" value="<?php echo $_POST['name'];?>"></td>
  132. </tr>
  133. <tr>
  134. <td>Email:</td>
  135. <td><input name="email" type="text" id="email" value="<?php echo $_POST['email'];?>"></td>
  136. </tr>
  137. <tr>
  138. <td>Reason for contact: </td>
  139. <td><select name="reason" id="reason" style="width:154px;">
  140. <?php if($_POST['reason'] == 'Support' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
  141. <option value="Support"<?=$sel;?>>Support</option>
  142. <?php if($_POST['reason'] == 'Billing'){ $sel = ' selected';} else { $sel = NULL;} ?>
  143. <option value="Billing"<?=$sel;?>>Billing</option>
  144. <?php if($_POST['reason'] == 'Complaints'){ $sel = ' selected';} else { $sel = NULL;} ?>
  145. <option value="Complaints"<?=$sel;?>>Complaints</option>
  146. <?php if($_POST['reason'] == 'Other'){ $sel = ' selected';} else { $sel = NULL;} ?>
  147. <option value="Other<?=$sel;?>">Other</option>
  148. </select></td>
  149. </tr>
  150. <tr>
  151. <td>Message:</td>
  152. <td><textarea name="message_text" cols="40" rows="4" id="message_text"><?php echo $_POST['message_text'];?></textarea></td>
  153. </tr>
  154. <?php
  155. if($IMAGE_VERIFICATION == 1){?>
  156. <tr>
  157. <td>Verification code:</td>
  158. <td> <table border="0" cellspacing="0" cellpadding="0">
  159. <tr>
  160. <td><?php
  161. $referenceid = md5(mktime()*rand());
  162. //Generate the random string
  163. $chars = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k",
  164. "K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v",
  165. "V","w","W","x","X","y","Y","z","Z","1","2","3","4","5","6","7","8","9");
  166. $length = 8;
  167. $textstr = "";
  168. for ($i=0; $i<$length; $i++) {
  169. $textstr .= $chars[rand(0, count($chars)-1)];
  170. }
  171. $new_string = RC4($textstr,$IMAGE_VER_CODE);
  172. $image_link = bin2hex($new_string);
  173. ?>
  174. <img src="sec_image.php?code=<?=$image_link;?>">
  175. <input name="hid_code" type="hidden" id="hid_code" value="<?=$image_link;?>"><br/>
  176. <input name="confirm_image" type="text" id="confirm_image" value="<?php echo $_POST['confirm_image'];?>"></td>
  177. </tr>
  178. </table>
  179. </td>
  180. </tr>
  181. <?php }
  182. if($FILE_UPLOAD == 1){?>
  183. <tr>
  184. <td>File Upload </td>
  185. <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  186. <tr>
  187. <td><input name="user_file" type="file" id="user_file"></td>
  188. </tr>
  189. <tr>
  190. <td><font color="#FF0000" size="2">Max. File Size:
  191. <?=$FILE_UPLOAD_MAX;?>
  192. MB, Allowed Extensions:
  193. <?php
  194. if($FILE_UPLOADS_EXT == 1){ echo 'All';}else{
  195. foreach($FILE_UPLOADS_EXT as $ext){ echo '-'.$ext;}}?>
  196. </font></td>
  197. </tr>
  198. </table> </td>
  199. </tr>
  200. <tr>
  201. <td colspan="2"><div align="center">
  202. <input type="submit" name="Submit" value="Send Contact">
  203. <input name="submit_id" type="hidden" id="submit_id" value="1">
  204. </div></td>
  205. </tr>
  206. <?php } ?>
  207. <tr>
  208. <td colspan="2"><div align="left"><a href="http://www.free-php-scripts.net" target="_blank"><font size="1">Powered by Simple Contact </font></a></div></td>
  209. </tr>
  210. </table>
  211. </form>
  212.  
  213. ?>


i moje pytanie: jak go przerobić aby wysyłał dwa załączniki? Męczę się nad tym od dobrych 10 godzin i już dostaję białej gorączki. Czytałem różne poradniki, z manualem php włącznie. Wiem, że muszę zastosować

  1. <td><input name="user_file[]" type="file" id="user_file"></td>


ale co dalej? Może ktoś mi pomóc? Będę bardzo wdzięczny.
Merko
Zainteresuj się klasą PHP Mailer bedzie bardziej przejrzyściej wysztstko wyglądało i będzie lepiej Zobacz tutaj smile.gif
Mistiqe
Dziękuję za odpowiedź, jednak to mnie nie urządza ponieważ chodzi mi konkretnie o ten o to skrypt. Nic innego...
Merko
a nie lepiej zrobic 
  1. <input name="user_file1" type="file" id="user_file1">
  2. <input name="user_file2" type="file" id="user_file2">

i powtórzyc to z skryptem chyba że chcesz miec nie 2 a wiecej zależnie od wymagań no to wtedy berdziej trzeba rozbudowac
Możesz też zaznaczyć 2 pliki przy wybieraniu ale nie wiem czy to pomoże
erix
Popraw temat na jakiś konkretny.
Mistiqe
Hmm...trochę pokombinowałem, zastosowałem się do tego co napisał Merko. Zmieniłem też następujący kod:

  1. if($message == NULL && $FILE_UPLOAD == 1 && $_FILES['user_file1']['name'] != NULL){
  2. if($_FILES['user_file1']['size'] > (($FILE_UPLOAD_MAX*1024)*1024)){ $message = 'File is over '.$FILE_UPLOAD_MAX.' MB in size.';}
  3. if($message == NULL && allowed_ext($FILE_UPLOADS_EXT,$_FILES['user_file1']['name']) == false){$message = 'Invalid extension.';}
  4. $new_filename1 = date("G_i_s_").$_FILES['user_file1']['name'];
  5. }
  6. if($message == NULL && $FILE_UPLOAD == 1 && $_FILES['user_file2']['name'] != NULL){
  7. if($_FILES['user_file2']['size'] > (($FILE_UPLOAD_MAX*1024)*1024)){ $message = 'File is over '.$FILE_UPLOAD_MAX.' MB in size.';}
  8. if($message == NULL && allowed_ext($FILE_UPLOADS_EXT,$_FILES['user_file2']['name']) == false){$message = 'Invalid extension.';}
  9. $new_filename = date("G_i_s_").$_FILES['user_file2']['name'];
  10. }


i

  1. if($FILE_UPLOAD == 1 && $_FILES['user_file1']['name'] != NULL){
  2. move_uploaded_file($_FILES['user_file1']['tmp_name'],$FILE_UPLOADS_DIR.$new_filename1);
  3. }
  4. if($FILE_UPLOAD == 1 && $_FILES['user_file2']['name'] != NULL){
  5. move_uploaded_file($_FILES['user_file2']['tmp_name'],$FILE_UPLOADS_DIR.$new_filename);
  6. }


Teraz oba pliki są wysyłane na serwer. Jeszcze tylko pozostaje opcja wyświetlenia ich jako załączniki w mailu. Tego już chyba nie wiem jak ugryźć:
  1. $fileatt_type = $_FILES['user_file1']['type'];
  2. $fileatt_type = $_FILES['user_file2']['type'];
  3.  
  4.  
  5.  
  6. $file = fopen($FILE_UPLOADS_DIR.$new_filename,'rb');
  7. while($dat = fread($file,1025657)){
  8. $attachment_data .= $dat;
  9. }
  10. fclose($file);
  11.  
  12. // Encode file content
  13. $attachment_data = chunk_split(base64_encode($attachment_data));
  14. //File upload headers
  15. $semi_rand = md5(time());
  16. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  17.  
  18. $headers = "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">";
  19.  
  20. // Add the headers for a file attachment
  21. $headers .= "\nMIME-Version: 1.0\n" .
  22. "Content-Type: multipart/mixed;\n" .
  23. " boundary=\"{$mime_boundary}\"";
  24.  
  25. // Add a multipart boundary above the plain message
  26. $new_message = "This is a multi-part message in MIME format.\n\n" .
  27. "--{$mime_boundary}\n" .
  28. "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=\"".$EMAIL_OPTIONS['CHARSET']."\"\n" .
  29. "Content-Transfer-Encoding: 7bit\n\n" .
  30. $admin_message . "\n\n";
  31.  
  32. // Add file attachment to the message
  33. $new_message .= "--{$mime_boundary}\n" .
  34. "Content-Type: {$fileatt_type};\n" .
  35. " name=\"{$new_filename}\"\n" .
  36. "Content-Disposition: attachment;\n" .
  37. " filename=\"{$new_filename}\"\n" .
  38. "Content-Transfer-Encoding: base64\n\n" .
  39. $attachment_data . "\n\n" .
  40. "--{$mime_boundary}--\n";
  41.  
  42. unset($attachment_data);


Zdublowałem wpisy i zmieniłem "new_filename" na "new_filename2" ale to chyba nie wystarczy. Nadal pokazuje jeden załącznik.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.