Php $\提交表单后,文件为空

Php $\提交表单后,文件为空,php,file-upload,Php,File Upload,我使用标准PHP函数上传一个文件,作为phpMail的附件 <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>

我使用标准PHP函数上传一个文件,作为phpMail的附件

<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 = "me@this.com";
    $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!";
?>

问题是未设置
$\u文件['upload']['name']
。以下是正在写入浏览器的回音:

刚得到表单值

将文本添加到电子邮件
目标=上传/
找不到用于上载的文件。
邮件生成…
邮件已发送

这使我认为我引用了文件上传字段或上传本身不正确


如果这不是最好的方法,是否有人可以在此处看到任何问题或提供一些指导?

enctype
添加到您的表单中: 更改:



您忘记在表单声明中设置多部分


在表单中添加
enctype=“多部分/表单数据”

您应该在表单中添加
enctype=multipart/表单数据

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

因为它是作为MIME流传输的,这是一种与普通POST非常不同的格式

您必须添加

enctype= "multipart/form-data"
把你的表格改成

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


如果您添加了enctype,但仍然无法工作,请检查PHP文件权限。在Oowebhost中,您可以在chmod中找到它。改为‘777’。否则,文件没有执行/写入的权限。

另一个提示:如果上载的文件大小小于用户选择的文件,则$\u文件将为空。

将enctype=“multipart/form data”添加到表单中^^^^^^^这是一个非常常见的错误,我想这可能是-当我看到答案时。这是我第一次使用文件上传,所以我想这次我可以原谅:DAwesome,文件正在上传。。。某种程度上。。。上传仍然失败,尽管。。。i、 e.
if(move_upload_file($_FILES['upload']['name'],$target_path))
返回false。apache的错误日志中没有任何线索……尽管在过去3年中我已经掌握了图像上传,但从2小时起我就一直在为同样的错误绞尽脑汁,结果证明我没有编写
enctype
。我觉得自己太傻了。这帮了我一下午的忙。但奇怪的是,我确信它过去没有这个也能用。
enctype= "multipart/form-data"
<form name="upload" method="post" action="send_form_email3.php" enctype= "multipart/form-data">