За последние 24 часа нас посетили 20463 программиста и 1011 роботов. Сейчас ищут 392 программиста ...

Не получается прикрепить файл для отправки формы

Тема в разделе "Прочие вопросы по PHP", создана пользователем podolskijspace, 15 июл 2020.

  1. podolskijspace

    podolskijspace Новичок

    С нами с:
    15 июл 2020
    Сообщения:
    1
    Симпатии:
    0
    Форма отлично отправляется, но никак не получается прикрепить туда файл, подскажите, в чем может быть проблема

    HTML:
    1.  
    2. <formid="form"action="send.php"enctype="multipart/form-data"class="footer__content"id="form1">
    3. <h2class="footer__title">Обратная связь</h2>
    4. <divclass="footer__subtitle">
    5. Заполните обязательные поля и мы начнем готовить ваш проект немедленно
    6. </div>
    7. <ulclass="footer__list-input">
    8.  
    9. <liclass="footer__item-input">
    10. <divclass="footer__wrapper-input">
    11. <labelfor=""class="input js-input">
    12. <inputname="userName"class="input__input"requiredtype="text">
    13. <divclass="input__placeholder">
    14. Имя
    15. </div>
    16.  
    17. </div>
    18. </li>
    19.  
    20. <liclass="footer__item-input">
    21. <divclass="footer__wrapper-input">
    22. <labelfor=""class="input js-input">
    23. <inputname="userPhone"class="input__input js-phone-mask"requiredtype="tel">
    24. <divclass="input__placeholder">
    25. Телефон
    26. </div>
    27.  
    28. </div>
    29. </li>
    30.  
    31. <liclass="footer__item-input">
    32. <divclass="footer__wrapper-input">
    33. <labelfor=""class="input js-input">
    34. <inputname="userEmail"class="input__input"requiredtype="email">
    35. <divclass="input__placeholder">
    36. Email
    37. </div>
    38.  
    39. </div>
    40. </li>
    41.  
    42. <liclass="footer__item-input">
    43. <divclass="footer__wrapper-input">
    44. <labelfor=""class="input js-input">
    45. <inputname="userWords"class="input__input"type="text">
    46. <divclass="input__placeholder">
    47. Комментарий к сообщению
    48. </div>
    49.  
    50. </div>
    51. </li>
    52.  
    53. </ul>
    54. <labelclass="add-file js-file">
    55. <inputname="upload"id="upload"type="file"class="add-file__input">
    56. <spanclass="add-file__desc">Добавить файл</span>
    57. <svgclass="icon icon-plus add-file__icon">
    58. <usexlink:href="#plus"></use>
    59. </svg>
    60.  
    61.  
    62. <buttontype="submit"class="btn footer__btn">
    63. <spanclass="btn__text">
    64. Отправить
    65. </span>
    66. </form>
    67.  
    68.  

    PHP:
    1. <?php
    2. $userName = $_POST['userName'];
    3.  
    4. // Import PHPMailer classes into the global namespace
    5. // These must be at the top of your script, not inside a function
    6. use PHPMailer\PHPMailer\PHPMailer;
    7. use PHPMailer\PHPMailer\SMTP;
    8. use PHPMailer\PHPMailer\Exception;
    9.  
    10. // Load Composer's autoloader
    11. require 'phpMailer/PHPMailer.php';
    12. require 'phpMailer/SMTP.php';
    13. require 'phpMailer/Exception.php';
    14.  
    15. $title = "Заголовок письма";
    16. $body = "
    17. <h2>Новое письмо</h2>
    18. <b>Имя:</b> $name<br>
    19. <b>Почта:</b> $email<br><br>
    20. <b>Сообщение:</b><br>$text
    21. ";
    22.  
    23. // Instantiation and passing `true` enables exceptions
    24. $mail = new PHPMailer();
    25.  
    26. try {
    27.     //Server settings
    28.     $mail->SMTPDebug = 0;                      // Enable verbose debug output
    29.     $mail->isSMTP();
    30.     $mail->CharSet = "UTF-8";                                            // Send using SMTP
    31.     $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
    32.     $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    33.     $mail->Username   = 'goncharovnikita713@gmail.com';                     // SMTP username
    34.     $mail->Password   = '***********';                               // SMTP password
    35.     $mail->SMTPSecure = 'ssl';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    36.     $mail->Port       = 465;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    37.  
    38.     //Recipients
    39.     $mail->setFrom('goncharovnikita713@gmail.com', 'Никита');
    40.     $mail->addAddress('HUJIbCOH@yandex.ru', 'Joe User');     // Add a recipient
    41.  
    42.     // Content
    43.     $mail->addAttachment($_FILES['upload']['tmp_name'], $_FILES['upload']['name']);
    44.     $mail->isHTML(true);                                 // Set email format to HTML
    45.     $mail->Subject = "Тема письма с файлом";
    46.     $mail->Body    = "Имя пользователя: ". $userName.;
    47.  
    48.  
    49.     $mail->send();
    50.     echo 'Отправлено';
    51. } catch (Exception $e) {
    52.     echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    53. }
     
  2. ADSoft

    ADSoft Старожил

    С нами с:
    12 мар 2007
    Сообщения:
    3.822
    Симпатии:
    736
    Адрес:
    Татарстан
    Прежде чем пытаться отправлять файл - его все же нужно загрузить
    почитайте про функцию для чего она используется, ну и используйте ее по назначению