我试图通过phpmailer上传文件。已经看过了一些文档和解决方案,但无法找到为什么要抛给我这个错误“未定义的数组键”附件.““试图访问null类型值上的数组偏移量”我使用browser的网络选项卡检查了我的有效负载,它清楚地显示了附件的存在。所有其他字段都按照预期完美地工作,接收所有字段的邮件,但无法获得附件,上载文件夹就在处理程序文件所在的当前目录中。我做错了什么?
这是我的前端部分:“
<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']}");
}
}发布于 2022-07-06 02:52:12
您忘了将enctype="multipart/form-data"放在表单标记中。文件只能在带有这种编码的表单中附加。
<form action="handler-att.php" method="post" enctype= "multipart/form-data">https://stackoverflow.com/questions/72877422
复制相似问题