在我检查以确保存在文件后,表单没有附加文件


Form not attaching file after I check to make sure there is a file present

我正在制作一个表单,提交备注并上传文件,并将其作为附件通过电子邮件发送。我遇到的问题是,如果我不附加文件,PHP就不知道该附加什么。所以为了解决这个问题,我改变了

  $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);      // attachment

收件人:

if($file != "")
      {
      $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);      // attachment
      }

现在,即使我把附件放进去,它也不会提交,我该如何解决这个问题?

完整代码:

<title>Success</title>
 <?php
    require_once '../PHPMailer_5.2.2/class.phpmailer.php';
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$gender = $_POST['gender'] ;
$recipientname = $_POST['recipientname'] ;
$hobbies = $_POST['hobbies'] ;
$age = $_POST['age'] ;
$school = $_POST['school'] ;
$pet = $_POST['pet'] ;
$hair = $_POST['hair'] ;
$eye = $_POST['eye'] ;
$food = $_POST['food'] ;
$drink = $_POST['drink'] ;
$sport = $_POST['sport'] ;
$schoolsubject = $_POST['schoolsubject'] ;
$teacher = $_POST['teacher'] ;
$strengths = $_POST['strengths'] ;
$animal = $_POST['animal'] ;
$superpower = $_POST['superpower'] ;
$fears = $_POST['fears'] ;
$skills = $_POST['skills'] ;
$dedication = $_POST['dedication'] ;
$friend = $_POST['friend'] ;
$otherinfo = $_POST['otherinfo'] ;
$file = $_POST['file'] ;
$body = "Name: $name
Email: $email
Gender: $gender
Recipient's name: $recipientname
Hobbies: $hobbies
Age: $age
School: $school
Pet Info: $pet
Hair Color: $hair
Eye Color: $eye
Favorite Food: $food
Favorite Drink $drink
Favorite Sport: $sport
Favorite School Subject: $schoolsubject
Favorite Teacher: $teacher
Strengths: $strengths
Favorite Animal: $animal
Favorite Superpower/Why: $superpower
Fears/Challenged: $fears
Skills: $skills
Dedication: $dedication
Best Friend Info: $friend
Other Information: $otherinfo";
    $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
    try {
      $mail->AddAddress('***@****.com', 'Michael');
      $mail->SetFrom($email, $name);
      $mail->AddReplyTo($email, $name);
      $mail->Subject = "Message From Legendmaker Customer: $name";
      $mail->Body = $body;
      $mail->Send();
      echo "Story Request Sent Successfully</p>'n";
      echo "<img src='/images/knight-success.png' alt='success' width='429' height='791' />"; 
      echo "Your request will be processed soon. Once your request has been processed you will receive an email. </p>'n";
    } catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
    }
    ?>

$_POST['file']似乎与此无关。

我会删除它并放入:

if ($_FILES['file']['size']) {
    ...
}

这样行吗?

检查$_POST['file']是否等于""不是检查用户是否上传文件的有效方法。您想要类似is_uploaded_file的内容。