2013-02-28 127 views
16

我使用标准PHP函数上传文件以用作PHPMailer的附件。

<form name="upload" method="post" action="send_form_email3.php"> 
    <div width="100%" class="con3"> 
     <div class="lable"> 
      <label for="first_name">First Name *</label> 
     </div> 
     <input type="text" name="first_name" id="first_name" class="span4"> 

     <div class="lable"> 
      <label for="email">Email Address *</label> 
     </div> 
     <input type="text" name="email" id="email" class="span4"> 

     <div class="lable"> 
      <label for="telephone">Contact Number *</label> 
     </div> 
     <input type="text" name="telephone" id="telephone" class="span4"> 

     <div class="lable"> 
      <label for="comments">Message *</label> 
     </div> 
     <textarea name="comments" rows="8" id="comments" class="span4"></textarea> 

     <div class="lable"> 
      <label for="upload">Send Us Your CV *</label> 
     </div> 
     <input type="file" name="upload" id="upload" /> 

     <input type="submit" value="Submit" class="btn btn-success"> 
    </div> 
</form> 

这种形式被提交到下面的PHP处理程序在此邮件是建立和发送:

<?php 

    require_once("class.phpmailer.php"); 

    $first_name = $_POST['first_name']; // required 
    $email_from = $_POST['email']; // required 
    $telephone = $_POST['telephone']; // required 
    $comments = $_POST['comments']; // required 

    echo "just got form values<br />"; 
    echo $_FILES['upload']['name'].'<br />'; 

    $email_message = "Form details below.<br />"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Contact: ".clean_string($telephone)."\n"; 
    $email_message .= "Message:\n".clean_string($comments)."\n"; 

    echo "added text to email<br />"; 

    $target_path = "uploads/"; 

    $target_path = $target_path . basename($_FILES['upload']['name']); 

    // upload the file 
    echo "target = ".$target_path."<br />"; 

    if (isset($_FILES['upload']['size'])) 
    { 
     if ($_FILES['upload']['size'] > 0) 
     { 
     echo 'file size: '.basename($_FILES['upload']['size']).'<br />'; 
      if (move_uploaded_file($_FILES['upload']['name'], $target_path)) 
      { 
       echo "The file ". basename($_FILES['upload']['name'])." has been uploaded<br />"; 
            // adding an already existing file as an attachment to the email for debugging purposes. 
       $email->AddAttachment('uploads/CreditReportViewer.pdf'); 
      } 
      else 
      { 
       echo "There was an error uploading the file, please try again!<br />&nbsp;".basename($_FILES['upload']['error']); 
      } 
     } 
     else 
     { 
      echo "There was an error uploading the file, please try again!<br />"; 
     } 
    } 
    else 
    { 
     echo "No file was found for the upload.<br />"; 
    } 


    $email = new PHPMailer(); 

    $email->To = "[email protected]"; 
    $email->From = $email_from; 
    $email->FromName = $first_name; 
    $email->Subject = "New Message from Website"; 
    $email->Body = $email_message; 

    echo "\n mail built...<br />"; 

    $email->Send(); 

    echo "mail sent!"; 
?> 

的问题是,$_FILES['upload']['name']未设置。这里是正在写入到浏览器的回声:

刚刚形成价值观

添加文本到电子邮件
目标=上传/
没有找到文件上传。
邮件建立...
邮件发送!

这让我觉得我引用文件上传字段或错误地上传本身。

任何人都可以在这里看到任何问题或提供一些指导,如果这不是最好的方式做到这一点?

+1

将enctype =“multipart/form-data”添加到您的表格 – 2013-02-28 07:49:05

+0

^^^非常常见的错误 – 2013-02-28 07:50:08

+0

Dagon我想这可能是 - 当我看到答案的时候。这是我第一次使用文件上传,但我想我可以原谅这一次:D – Ortund 2013-02-28 08:33:44

回答

36

您必须添加,

enctype= "multipart/form-data" 

更改您的形式,

传输
<form name="upload" method="post" action="send_form_email3.php" enctype= "multipart/form-data"> 
+0

尽管在过去3年掌握了图片上传,但是我仍然在为自己的头撞到2小时后的相同错误,结果我没有编写'enctype'。感觉如此愚蠢.. – coder101 2014-12-27 15:26:57

+0

整个下午把我的头发撕掉后,这帮助了我。但奇怪的是,我确信它过去没有这个工作。 – 2015-02-12 15:39:03

6

添加enctype到您的窗体: 变化:

<form name="upload" method="post" action="send_form_email3.php"> 

<form name="upload" method="post" action="send_form_email3.php" enctype="multipart/form-data"> 
2

你忘记设置多形式的声明。

add enctype="multipart/form-data" in form。

2

您应该将enctype=multipart/form-data添加到您的表单中。

<form name="upload" method="post" action="send_form_email3.php" enctype="multipart/form-data"> 

因为它是作为一个MIME流,一个非常不同的格式比正常POST

What does enctype='multipart/form-data' mean?

+0

太棒了,文件正在上传...排序...上传仍然失败,虽然... ie if(if move_uploaded_file($ _ FILES ['upload'] ['name'],$ target_path))'返回false。虽然没有在Apache的错误日志中的线索,虽然... – Ortund 2013-02-28 08:00:24

0

如果您添加了enctype并仍然不起作用 - 请检查PHP文件权限。在ooowebhost中,你可以在chmod中找到它。改为'777'。否则,该文件没有执行/写入权限

5

另一个提示:如果upload_max_filesize(php.ini)小于用户选择的文件,$ _FILES将为空。