doudeng2025 2017-10-24 02:21
浏览 68
已采纳

PHPMailer附件路径文件名不显示附件

PHPMailer attachment issue:

When I do a var_dump($_FILES), it displays all the correct content within the array, and also moves the uploaded file to my attachments folder, so this part works perfectly in my form. I added the form attribute enctype="multipart/form-data" to encode the form data over the server.

So, my issue is trying to display the $_files['attachment'] pathfile name to the page using echo $file after the form submitted. This is not working and I dont know why?

To give a background of my file structure:

  1. main directory folder: mailer-for-php
  2. folder within: attachments
  3. folder within: images
  4. directory folder within: vendor (with associated composer files and PHPMailer
  5. sendmail-v2.php

Here is my PHP code:

<?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 code:

<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>
  • 写回答

1条回答 默认 最新

  • dplase3140 2017-10-24 02:28
    关注

    You are putting echo in your else statement. Put it outside else like this

    <?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
    
    ?>
    

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助