Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Wczytanie danych z pliku i wysłanie formularzem
Forum PHP.pl > Forum > Przedszkole
ben.gannon
Witajcie.
W którym miejscu kodu formularza wstawić kod odpowiedzialny za czytanie zadanego pliku txt aby jego zawartość została wysłana

formularz
  1. <?php
  2. function ValidateEmail($email)
  3. {
  4. $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
  5. return preg_match($pattern, $email);
  6. }
  7.  
  8. if($_SERVER['REQUEST_METHOD'] == 'POST')
  9. {
  10. $mailto = 'moj.email@o2.pl';
  11. $mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
  12. $subject = 'Contact Information';
  13. $message = 'Values submitted from web site form:';
  14. $success_url = '';
  15. $error_url = '';
  16. $error = '';
  17. $eol = "\n";
  18. $max_filesize = isset($_POST['filesize']) ? $_POST['filesize'] * 1024 : 1024000;
  19. $boundary = md5(uniqid(time()));
  20.  
  21. $header = 'From: '.$mailfrom.$eol;
  22. $header .= 'Reply-To: '.$mailfrom.$eol;
  23. $header .= 'MIME-Version: 1.0'.$eol;
  24. $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
  25. $header .= 'X-Mailer: PHP v'.phpversion().$eol;
  26. if (!ValidateEmail($mailfrom))
  27. {
  28. $error .= "The specified email address is invalid!\n<br>";
  29. }
  30.  
  31. if (!empty($error))
  32. {
  33. $errorcode = file_get_contents($error_url);
  34. $replace = "##error##";
  35. $errorcode = str_replace($replace, $error, $errorcode);
  36. echo $errorcode;
  37. }
  38.  
  39. $internalfields = array ("submit", "reset", "send", "captcha_code");
  40. $message .= $eol;
  41. $message .= "IP Address : ";
  42. $message .= $_SERVER['REMOTE_ADDR'];
  43. $message .= $eol;
  44. foreach ($_POST as $key => $value)
  45. {
  46. if (!in_array(strtolower($key), $internalfields))
  47. {
  48. if (!is_array($value))
  49. {
  50. $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
  51. }
  52. else
  53. {
  54. $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
  55. }
  56. }
  57. }
  58.  
  59. $body = 'This is a multi-part message in MIME format.'.$eol.$eol;
  60. $body .= '--'.$boundary.$eol;
  61. $body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol;
  62. $body .= 'Content-Transfer-Encoding: 8bit'.$eol;
  63. $body .= $eol.stripslashes($message).$eol;
  64. if (!empty($_FILES))
  65. {
  66. foreach ($_FILES as $key => $value)
  67. {
  68. if ($_FILES[$key]['error'] == 0 && $_FILES[$key]['size'] <= $max_filesize)
  69. {
  70. $body .= '--'.$boundary.$eol;
  71. $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;
  72. $body .= 'Content-Transfer-Encoding: base64'.$eol;
  73. $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;
  74. $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;
  75. }
  76. }
  77. }
  78. $body .= '--'.$boundary.'--'.$eol;
  79. mail($mailto, $subject, $body, $header);
  80. header('Location: '.$success_url);
  81. }
  82. ?>



czytanie pliku
  1. $plik = file_get_contents('plik.txt');
  2. echo $plik;


Pozdrawiam.
thek
A to nie jest oczywiste? Zobacz na parametry funkcji mail i zastanów po co jest tam body. To, czy zdecydujesz się ów plik dodać jakoo załacznik, czy jako plain text tylko nieco zmieni formę tego JAK to zrobisz.
ben.gannon
Dziękuję za zainteresowanie moją sprawą!

Jestem w tym zupełnie zielony czy mógłbyś uzupełnić tę część odpowiedzialną za wykonanie tego i podać mi jako przykład, np aby po wypełnieniu pol formularza i podaniu maila na ten własnie mail byly wysylane dane z pliku txt na 20 znaków?
thek
Nie będę Ci pisał gotowca, bo gdybym dał Ci przykład to tak by się to skończyło. Podpowiem tylko, że masz się zainteresowac bliżej linijkami 60-62 w kodzie powyżej. Ucz się, a nie szukaj jedynie gotowców. Wyjdziesz na tym lepiej w przyszłości. Nie będziesz latał z byle pierdółką na forum.
ben.gannon
Poprawiłem, dodałem część odpowiedzialną za generowanie unikalnego kodu (numeru formularza)
  1. $znaki = 'abcdefghijk1234567890'; // tutaj wymieniasz wszystkie mozliwe znaki
  2. $znaki_c = count($znaki); // ilosc znakow
  3. $kod='';
  4. for($i=0; $i<15; $i++){
  5. $kod.=$znaki[rand(0,$znaki_c-1)]; // tworzenie kodu
  6. }


i calość ma być wysyłana na mail jaki podaje user w formularzu (no i do mnie oczywiście)
  1. mail($message, $subject, $body, $header, $kod);


niestety nie działa - co zawalilem?, proszę o pomoc...

cały kod:
  1. <?php
  2. function ValidateEmail($email)
  3. {
  4. $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
  5. return preg_match($pattern, $email);
  6. }
  7.  
  8. if($_SERVER['REQUEST_METHOD'] == 'POST')
  9. {
  10. $mailto = 'moj_email@o2.pl';
  11. $mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
  12. $subject = 'Website form';
  13. $message = 'Values submitted from web site form:';
  14. $success_url = '';
  15. $error_url = '';
  16. $error = '';
  17. $eol = "\n";
  18. $max_filesize = isset($_POST['filesize']) ? $_POST['filesize'] * 1024 : 1024000;
  19. $boundary = md5(uniqid(time()));
  20.  
  21. $header = 'From: '.$mailfrom.$eol;
  22. $header .= 'Reply-To: '.$mailfrom.$eol;
  23. $header .= 'MIME-Version: 1.0'.$eol;
  24. $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
  25. $header .= 'X-Mailer: PHP v'.phpversion().$eol;
  26. if (!ValidateEmail($mailfrom))
  27. {
  28. $error .= "The specified email address is invalid!\n<br>";
  29. }
  30.  
  31. if (!empty($error))
  32. {
  33. $errorcode = file_get_contents($error_url);
  34. $replace = "##error##";
  35. $errorcode = str_replace($replace, $error, $errorcode);
  36. echo $errorcode;
  37. }
  38.  
  39. $internalfields = array ("submit", "reset", "send", "captcha_code");
  40. $message .= $eol;
  41. $message .= "IP Address : ";
  42. $message .= $_SERVER['REMOTE_ADDR'];
  43. $message .= $eol;
  44. foreach ($_POST as $key => $value)
  45. {
  46. if (!in_array(strtolower($key), $internalfields))
  47. {
  48. if (!is_array($value))
  49. {
  50. $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
  51. }
  52. else
  53. {
  54. $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
  55. }
  56. }
  57. }
  58. $znaki = 'abcdefghijk1234567890'; // wszystkie mozliwe znaki
  59. $znaki_c = count($znaki); // ilosc znakow
  60. $kod='';
  61. for($i=0; $i<15; $i++){
  62. $kod.=$znaki[rand(0,$znaki_c-1)]; // tworzenie kodu
  63. }
  64.  
  65. $body = 'This is a multi-part message in MIME format.'.$eol.$eol;
  66. $body .= '--'.$boundary.$eol;
  67. $body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol;
  68. $body .= 'Content-Transfer-Encoding: 8bit'.$eol;
  69. $body .= $eol.stripslashes($message).$eol;
  70. if (!empty($_FILES))
  71. {
  72. foreach ($_FILES as $key => $value)
  73. {
  74. if ($_FILES[$key]['error'] == 0 && $_FILES[$key]['size'] <= $max_filesize)
  75. {
  76. $body .= '--'.$boundary.$eol;
  77. $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;
  78. $body .= 'Content-Transfer-Encoding: base64'.$eol;
  79. $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;
  80. $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;
  81. }
  82. }
  83. }
  84. $body .= '--'.$boundary.'--'.$eol;
  85. mail($mailto, $subject, $body, $header, $kod);
  86. mail($message, $subject, $body, $header, $kod);
  87. header('Location: '.$success_url);
  88. }
  89. ?>
  90. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  91. <html>
  92. <head>
  93. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  94. <title>Lapu</title>
  95. <meta name="generator" content="Lapu">
  96. <style type="text/css">
  97. body
  98. {
  99. background-color: #FFFFFF;
  100. color: #000000;
  101. }
  102. </style>
  103. <style type="text/css">
  104. p, span, div, ol, ul, li, td, button, input, textarea, form
  105. {
  106. margin: 0;
  107. padding: 0;
  108. }
  109. a
  110. {
  111. color: #C8D7EB;
  112. outline: none;
  113. text-decoration: underline;
  114. }
  115. a:visited
  116. {
  117. color: #C8D7EB;
  118. }
  119. a:active
  120. {
  121. color: #C8D7EB;
  122. }
  123. a:hover
  124. {
  125. color: #376BAD;
  126. text-decoration: underline;
  127. }
  128. </style>
  129. <link rel="stylesheet" href="./wb.validation.css" type="text/css">
  130. <style type="text/css">
  131. #wb_Form1
  132. {
  133. background-color: #FAFAFA;
  134. border: 0px #000000 solid;
  135. }
  136. #Editbox1
  137. {
  138. border: 1px #C0C0C0 solid;
  139. background-color: #FFFFFF;
  140. color :#000000;
  141. font-family: Arial;
  142. font-size: 13px;
  143. text-align: left;
  144. vertical-align: middle;
  145. }
  146. #wb_Text1
  147. {
  148. background-color: transparent;
  149. border: 0px #000000 solid;
  150. padding: 0;
  151. }
  152. #wb_Text1 div
  153. {
  154. text-align: left;
  155. }
  156. #Button1
  157. {
  158. color: #000000;
  159. font-family: Arial;
  160. font-size: 13px;
  161. }
  162. </style>
  163. <script type="text/javascript" src="./jquery-1.6.4.min.js"></script>
  164. <script type="text/javascript" src="./wb.validation.min.js"></script>
  165. <script type="text/javascript">
  166. $(document).ready(function()
  167. {
  168. $("#Form1").submit(function(event)
  169. {
  170. var isValid = $.validate.form(this);
  171. return isValid;
  172. });
  173. $("#Editbox1").validate(
  174. {
  175. required: false,
  176. type: 'email',
  177. color_text: '#000000',
  178. color_hint: '#00FF00',
  179. color_error: '#FF0000',
  180. color_border: '#808080',
  181. nohint: false,
  182. font_family: 'Arial',
  183. font_size: '13px',
  184. position: 'topleft',
  185. offsetx: 0,
  186. offsety: 0,
  187. effect: 'none',
  188. error_text: 'NIE WPISALES MAILA'
  189. });
  190. });
  191. </script>
  192. </head>
  193. <body>
  194. <div id="wb_Form1" style="position:absolute;left:75px;top:91px;width:410px;height:196px;z-index:3;">
  195. <form name="Form1" method="post" action="<?php echo basename(__FILE__); ?>" enctype="multipart/form-data" id="Form1">
  196. <input type="text" id="Editbox1" style="position:absolute;left:149px;top:39px;width:167px;height:19px;line-height:19px;z-index:0;" name="Editbox1" value="">
  197. <div id="wb_Text1" style="position:absolute;left:99px;top:42px;width:40px;height:16px;z-index:1;">
  198. <span style="color:#000000;font-family:Arial;font-size:13px;">email</span></div>
  199. <input type="submit" id="Button1" name="" value="Wyslij" style="position:absolute;left:98px;top:112px;width:96px;height:25px;z-index:2;">
  200. </form>
  201. </div>
  202. </body>
  203. </html>
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.