PHPMailer附件问题:
当我执行var_dump($ _ FILES)时,它会显示数组中的所有正确内容,并将上传的文件移动到我的附件文件夹,因此这部分在我的表单中完美运行。我添加了表单属性enctype =“multipart / form-data”来对服务器上的表单数据进行编码。
所以,我的问题是在提交表单后尝试使用echo $ file向页面显示$ _files ['attachment']路径文件名。这不起作用,我不知道为什么?
提供我的文件结构背景:
这是我的PHP代码:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
//var_dump($_FILES);
if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] !="") {
$file = "attachments/" . basename($_FILES['attachment']['name']);
move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
} else{
$file = " ";
echo $file;
}
} // end of post submit
?>
HTML表单代码:
<form method="post" action="sendmail-v2.php" enctype="multipart/form-data">
<input type="text" class="form-control" name="name" placeholder="name">
<input type="text" class="form-control" name="subject" placeholder="subject">
<input type="email" class="form-control" name="email" placeholder="email">
<textarea class="form-control" name="message" placeholder="enter message here" rows="3"></textarea>
<input type="file" name="attachment" class="file-input form-control d-block mb-4" id="attachment-file">
<!-- display pathfile selected to user -->
<!-- <script type="text/javascript">
document.getElementById('attachment-file').onchange = function () {
alert('Selected file: ' + this.value);
};
</script> -->
<input class="btn btn-primary" type="submit" name="submit" value="send email">
</form>
答案 0 :(得分:1)
你在你的else语句中加入了echo。把它放在其他地方就像这样
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
//var_dump($_FILES);
if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] !="") {
$file = "attachments/" . basename($_FILES['attachment']['name']);
move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
} else{
$file = " ";
}
echo $file;
} // end of post submit
?>
希望这有帮助