php - $_FILES 表单提交后为空

标签 php file-upload

我正在使用标准的 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 = "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!";
?>

问题是 $_FILES['upload']['name'] 没有设置。这是正在写入浏览器的回显:

just got form values

added text to email
target = uploads/
No file was found for the upload.
mail built...
mail sent!

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

如果这不是执行此操作的最佳方法,任何人都可以在这里看到任何问题或提供一些指导吗?

最佳答案

你必须添加,

enctype= "multipart/form-data"

将您的表单更改为,

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

关于php - $_FILES 表单提交后为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130159/

相关文章:

php - 将 PHP cURL 请求从 SSLv3 更新为 TLS..?

php - Laravel Blade 中不显示 session 值

javascript - Dropzone.js - 上传后的成功消息

php - Laravel 自定义验证消息参数

php - Stripe 连接收费返回错误 (PHP)

php - 在sql中存储html元素的有效方法

php - fopen() : Filename cannot be empty - Laravel 5 IIS 7

codeigniter 3-您没有选择要上传的文件

ruby-on-rails - Rails 验证错误后如何保留文件上传字段

jquery-plugins - jQuery 文件上传不再调整图像大小