phpmailer文件上传的问题是什么?

发布于 2025-02-13 03:38:31 字数 4442 浏览 0 评论 0原文

我正在尝试通过phpmailer上传文件来发送文件。已经浏览了几个文档和解决方案,但找不到为什么它会给我带来此错误“未定义的数组键“附件” ..........和“试图访问dray offs of Type null” i的值使用浏览器的网络选项卡检查了我的有效载荷,并且清楚地显示了附件。每个其他字段都按预期工作完美,接收所有字段,但无法获取附件,上传文件夹就在处理程序文件所在的当前目录内。 我在做什么错?

这是我的前端部分:“

    <form action="handler-att.php" method="post">
     <!--the first and last names -->
     <div class="row mb-4">
         <div class="col-lg-6 col-sm-12">
             <div class="form-outline">
                 <input id="first-name" class="form-control" required="" name="first_name" placeholder="First Name*"
                     type="text" />
                 <!-- <label class="form-label" for="first-name">First name</label> -->
             </div>
         </div>
         <div class="col-lg-6 col-sm-12">
             <div class="form-outline">
                 <input id="last-name" class="form-control" required="" name="last_name" placeholder="Last Name*"
                     type="text" />
                 <!-- <label class="form-label" for="last-name">Last name</label> -->
             </div>
         </div>
     </div>

     <!-- Email Mobile input -->
     <div class="row mb-4">
         <div class="col-lg-6 col-sm-12">
             <div class="form-outline">
                 <input id="register-email" class="form-control" name="register_email" required
                     placeholder="Email Address*" type="email" />
                 <!-- <label class="form-label" for="register-email">Email</label> -->
             </div>
         </div>
         <div class="col-lg-6 col-sm-12">
             <div class="form-outline">
                 <input id="mobile-number" class="form-control" name="mobile_number" required
                     placeholder="Mobile Number*" type="tel" maxlength="10" pattern="^[6-9]\d{9}$"
                     title="Please enter a valid number" />
                 <!-- <label class="form-label" for="mobile-number">Phone</label> -->
             </div>
         </div>
     </div>

     <!-- Dropdown input -->
     <div class="row mb-4">
         <div class="col-lg-12 col-sm-12">
             <div class="form-outline">
                 <select id="college-select" class="form-select" aria-label="Default select example" required
                     name="course_preference">
                     <option value="" selected disabled hidden="">Select the course you are interested in</option>

                     <option value="B.Ed">B.Ed</option>
                     <option value="M.Ed">M.Ed</option>
                     <option value="D.El.Ed">D.El.Ed</option>
                     <option value="GNM Nursing">GNM Nursing</option>
                     <option value="B.Sc Nursing">B.Sc Nursing</option>
                     <option value="Engineering">Engineering</option>
                 </select>
                 <!-- <label class="form-label" for="college-select">Select College</label> -->
             </div>
         </div>

     </div>

     <!-- ===File attachemnt==== -->
     <input type="file" id="attachment" name="attachment">

     <!-- Message input -->
     <div class="form-outline mb-4">
         <textarea class="form-control" id="description" name="description" rows="6" required></textarea>
         <!-- <label class="form-label" for="description">Tell Us About Yourself</label> -->
     </div>

     <!-- Submit button -->
     <button type="submit" class="btn-lg btn-primary btn-block mb-4 contact-btn ps-5 pe-5">Send Enquiry &nbsp;
         &nbsp; &nbsp;<span class="bi-arrow-right"></span></button>
 </form>

这是我的phpmailer代码的附件部分:

   if($_FILES['attachment']['name']!=null){
      if(move_uploaded_file($_FILES['attachment']['tmp_name'], "uploads/{$_FILES['attachment']['name']}"))
         {
          $mail->addAttachment("uploads/{$_FILES['attachment']['name']}");
      }
      }

I'm trying to send files by uploading it through phpmailer. Already gone through a couple of documentation and solution but not able to find why it is throwing me this error "Undefined array key "attachment"......." and "Trying to access array offset on value of type null" I checked my payload using browser's network tab and it's clearly showing the attachment is present. Every other field is working perfectly as intended, receiving the mail with all the fields but not able get the attachment, the uploads folder is just there inside the current directory where the handler file is located.
What am I doing wrong?

Here's my front end part:"

    <form action="handler-att.php" method="post">
     <!--the first and last names -->
     <div class="row mb-4">
         <div class="col-lg-6 col-sm-12">
             <div class="form-outline">
                 <input id="first-name" class="form-control" required="" name="first_name" placeholder="First Name*"
                     type="text" />
                 <!-- <label class="form-label" for="first-name">First name</label> -->
             </div>
         </div>
         <div class="col-lg-6 col-sm-12">
             <div class="form-outline">
                 <input id="last-name" class="form-control" required="" name="last_name" placeholder="Last Name*"
                     type="text" />
                 <!-- <label class="form-label" for="last-name">Last name</label> -->
             </div>
         </div>
     </div>

     <!-- Email Mobile input -->
     <div class="row mb-4">
         <div class="col-lg-6 col-sm-12">
             <div class="form-outline">
                 <input id="register-email" class="form-control" name="register_email" required
                     placeholder="Email Address*" type="email" />
                 <!-- <label class="form-label" for="register-email">Email</label> -->
             </div>
         </div>
         <div class="col-lg-6 col-sm-12">
             <div class="form-outline">
                 <input id="mobile-number" class="form-control" name="mobile_number" required
                     placeholder="Mobile Number*" type="tel" maxlength="10" pattern="^[6-9]\d{9}
quot;
                     title="Please enter a valid number" />
                 <!-- <label class="form-label" for="mobile-number">Phone</label> -->
             </div>
         </div>
     </div>

     <!-- Dropdown input -->
     <div class="row mb-4">
         <div class="col-lg-12 col-sm-12">
             <div class="form-outline">
                 <select id="college-select" class="form-select" aria-label="Default select example" required
                     name="course_preference">
                     <option value="" selected disabled hidden="">Select the course you are interested in</option>

                     <option value="B.Ed">B.Ed</option>
                     <option value="M.Ed">M.Ed</option>
                     <option value="D.El.Ed">D.El.Ed</option>
                     <option value="GNM Nursing">GNM Nursing</option>
                     <option value="B.Sc Nursing">B.Sc Nursing</option>
                     <option value="Engineering">Engineering</option>
                 </select>
                 <!-- <label class="form-label" for="college-select">Select College</label> -->
             </div>
         </div>

     </div>

     <!-- ===File attachemnt==== -->
     <input type="file" id="attachment" name="attachment">

     <!-- Message input -->
     <div class="form-outline mb-4">
         <textarea class="form-control" id="description" name="description" rows="6" required></textarea>
         <!-- <label class="form-label" for="description">Tell Us About Yourself</label> -->
     </div>

     <!-- Submit button -->
     <button type="submit" class="btn-lg btn-primary btn-block mb-4 contact-btn ps-5 pe-5">Send Enquiry  
            <span class="bi-arrow-right"></span></button>
 </form>

And here's the attachment part of my phpmailer code:

   if($_FILES['attachment']['name']!=null){
      if(move_uploaded_file($_FILES['attachment']['tmp_name'], "uploads/{$_FILES['attachment']['name']}"))
         {
          $mail->addAttachment("uploads/{$_FILES['attachment']['name']}");
      }
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2025-02-20 03:38:32

您忘了将Enctype =“ Multipart/form-data”放在表单标签中。文件只能与该编码一起以表格附加。

  <form action="handler-att.php" method="post" enctype= "multipart/form-data">

You forgot to put the enctype="multipart/form-data" in the form tag. Files can only be attached in forms with that encoding.

  <form action="handler-att.php" method="post" enctype= "multipart/form-data">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文