Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Wczytanie danych z pliku i wysłanie formularzem, czytanie pliku txt w php
ben.gannon
post
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 9.01.2012

Ostrzeżenie: (0%)
-----


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.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
thek
post
Post #2





Grupa: Moderatorzy
Postów: 4 362
Pomógł: 714
Dołączył: 12.02.2009
Skąd: Jak się położę tak leżę :D




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.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 17.10.2025 - 13:11