phpmailer文件上传的问题是什么?
我正在尝试通过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
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘了将
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.