Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]walidacja załącznika, wysyła mimo warunku
jaroslavmo
post
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 21.03.2022

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


Użyłem php mailer do wysyłania emaila, chciałbym zrobić walidację wielkości pliku po stronie serwera.

stowrzyłem taki warunek

  1.  
  2. $maxsize = 2*1024*1024
  3.  
  4. if(isset($_Files['file']['name'])) {
  5.  
  6. if($_FILES['file']['size'] > $maxsize) {
  7. echo 'zbyt duży plik';
  8.  
  9. } else {
  10. $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
  11. }
  12.  
  13. }
  14.  
  15.  
  16.  


mimo tego mail się wysyła bez załącznika, ale wysyła. Chciałbym to zmienić.
Poniżej cały mój kod php





  1.  
  2.  
  3.  
  4. <?php
  5. ini_set('display_errors', '1');
  6. ini_set('display_startup_errors', '1');
  7.  
  8. $honeypot = $_POST['honey'];
  9. $nameU = $_POST['name'];
  10. $email = $_POST['email'];
  11. $content = $_POST['message'];
  12. $phoneU = $_POST['phone'];
  13. $fileUpload = $_POST['file'];
  14.  
  15. use PHPMailer\PHPMailer\PHPMailer;
  16. use PHPMailer\PHPMailer\SMTP;
  17. use PHPMailer\PHPMailer\Exception;
  18.  
  19. require 'autoload.php';
  20.  
  21. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  22. // robot detection
  23. $honeypot = trim($_POST["honey"]);
  24.  
  25. if(!empty($honeypot)) {
  26. echo "NO SPAM!";
  27. }
  28.  
  29. $mail = new PHPMailer; //From email address and name
  30. $mail->From = $email ;
  31.  
  32. $mail->FromName = $nameU; //To address and name
  33. $mail->addAddress("jaroslaw.mor@gmail.com", "Vomo");//Recipient name is optional
  34.  
  35. $mail->isHTML(true);
  36. $mail->Subject = "Zapytanie ze strony www";
  37. $mail->Body = "Telefon:$phoneU<br><br>Treść wiadomośći:<br>$content";
  38. $mail->AltBody = "Telefon:$phoneU\n$content";
  39.  
  40. $maxsize = 2*1024*1024;
  41.  
  42.  
  43.  
  44. if(isset($_Files['file']['name'])) {
  45.  
  46. if($_FILES['file']['size'] > $maxsize) {
  47. echo 'zbyt duży plik';
  48.  
  49. } else {
  50. $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
  51. }
  52.  
  53. }
  54.  
  55.  
  56.  
  57. if(!$mail->send())
  58. {
  59. echo "Mailer Error: " . $mail->ErrorInfo;
  60. }
  61. else
  62. {
  63. header("Location: sent.html");
  64. exit();
  65. }
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  

Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
jaroslavmo
post
Post #2





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 21.03.2022

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


Ok, poradziłem sobie z załącznikiem i działało a teraz mam taki dziwny problem że każdy załącznik, nawet jeśli jest mniejszy od maxsize to i tak pokazuje że plik jest zbyt duży i wykonuje się instrukcja dla zbyt dużego pliku.





  1.  
  2.  
  3.  
  4. <?php
  5. //Import PHPMailer classes into the global namespace
  6. //These must be at the top of your script, not inside a function
  7. use PHPMailer\PHPMailer\PHPMailer;
  8. use PHPMailer\PHPMailer\SMTP;
  9. use PHPMailer\PHPMailer\Exception;
  10.  
  11. //Load Composer's autoloader
  12. require 'autoload.php';
  13.  
  14. //Create an instance; passing `true` enables exceptions
  15. $mail = new PHPMailer(true);
  16. $honeypot = $_POST['honey'];
  17. $user_name = $_POST['name'];
  18. $user_email = $_POST['email'];
  19. $user_message = $_POST['message'];
  20. $user_phone = $_POST['phone'];
  21. $honeypot = trim($_POST["honey"]);
  22. $max_size = 2 * 1024 * 1204; //2mb
  23. $attachment = $_FILES['uploaded-file'];
  24.  
  25. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  26.  
  27.  
  28. if(!empty($honeypot)) {
  29. echo "NO SPAM!";
  30. } else {
  31.  
  32. $mail = new PHPMailer; //From email address and name
  33. $mail->isMail();
  34.  
  35. //sender
  36. $mail->From = $user_email;
  37. $mail->FromName = $user_name;
  38.  
  39. //recipient
  40. $mail->addAddress("jaroslaw.mor@gmail.com");
  41.  
  42.  
  43. //mail subject
  44. $mail->Subject = "Zapytanie ze strony www";
  45.  
  46.  
  47. $mail->isHTML(true);
  48. //body mail
  49. $mail->Body = "Telefon:$user_phone<br><br>Treść wiadomośći:<br>$user_message";
  50. $mail->AltBody = "Telefon:$user_phone\n$content";
  51.  
  52. //attachment
  53. if(isset($attachment)) {
  54.  
  55. for ($i = 0; $i < count($_FILES['uploaded-file']['name']); $i++) {
  56. if ($_FILES['uploaded-file']['error'][$i] !== UPLOAD_ERR_OK) continue;
  57. $file_TmpName = $_FILES['uploaded-file']["tmp_name"][$i];
  58. $file_name = $_FILES['uploaded-file']["name"][$i];
  59.  
  60. if ($_FILES['uploaded-file']['size'][$i] > $maxsize) {
  61.  
  62. $message = urlencode("załącznik przekorczył limit 10MB");
  63. header("Location:".$_SERVER['HTTP_REFERER']. '#file-size-error');
  64. die();
  65.  
  66. }
  67.  
  68. else{
  69. move_uploaded_file($fileTmpName, "uploads/" . $filename);
  70. $mail-> AddAttachment("uploads/". $filename);
  71. }
  72. }//for
  73. }//isset
  74.  
  75. if(!$mail->send()) {
  76. echo "Mailer Error: " . $mail->ErrorInfo;
  77. exit();
  78. }
  79.  
  80. else {
  81. header("Location: sent.html");
  82.  
  83. exit();
  84.  
  85. }//if send else
  86.  
  87. }//honey else end
  88.  
  89. }//post end
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
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: 27.12.2025 - 16:49