表单提交后$ _FILES为空 [英] $_FILES empty after form submission

查看:88
本文介绍了表单提交后$ _FILES为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <表单名称

使用标准的PHP函数上传文件以作为PHPMailer的附件使用。 =uploadmethod =postaction =send_form_email3.php>
< div width =100%class =con3>
< div class =lable>
< label for =first_name>名字*< / label>
< / div>
< input type =textname =first_nameid =first_nameclass =span4>

< div class =lable>
< label for =email>电子邮件地址*< / label>
< / div>
< input type =textname =emailid =emailclass =span4>

< div class =lable>
< label for =telephone>电话号码*< / label>
< / div>
< input type =textname =telephoneid =telephoneclass =span4>

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

< div class =lable>
< label for =upload>向我们发送您的简历*< / label>
< / div>
< input type =filename =uploadid =upload/>

< input type =submitvalue =Submitclass =btn btn-success>
< / div>
< / form>

这个表单被提交到下面的PHP处理程序,在这个处理程序中构建和发送邮件:

 <?php 

require_once(class.phpmailer.php);

$ first_name = $ _POST ['first_name']; //必需
$ email_from = $ _POST ['email']; //必需
$ telephone = $ _POST ['telephone']; //必需
$ comments = $ _POST ['comments']; //需要

echo刚刚获得表单值< br />;
echo $ _FILES ['upload'] ['name']。'< br />';

$ email_message =下面的表格详细信息。< br />;
$ b $函数clean_string($ string){
$ bad = array(content-type,bcc:,to:,cc:,href);
返回str_replace($ bad,,$ string);
}

$ email_message。=名称:.clean_string($ first_name)。\\\
;
$ email_message。=Email:.clean_string($ email_from)。\\\
;
$ email_message。=联系人:.clean_string($ telephone)。\\\
;
$ email_message。=消息:\ n.clean_string($ comments)。\\\
;

回显添加文本到电子邮件< br />;

$ target_path =uploads /;

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

//上传文件
echotarget =。$ target_path。< br />;
$ b $ 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))
{
echoThe file。 basename($ _FILES ['upload'] ['name'])。已上传< br />;
//将已经存在的文件作为电子邮件的附件添加到调试目的中。
$ email-> AddAttachment('uploads / CreditReportViewer.pdf');
}
else
{
echo上传文件时出现错误,请重试!< br />& nbsp;。basename($ _ FILES [ '上传'] [ '错误']);


else

echo上传文件时出现错误,请重试!< br />;


else

echo没有找到上传的文件。< br />;
}


$ email = new PHPMailer();

$ email-> To =me@this.com;
$ email-> From = $ email_from;
$ email-> FromName = $ first_name;
$ email-> Subject =来自网站的新留言;
$ email-> Body = $ email_message;

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

$ email->发送();

echomail sent!;
?>

问题在于 $ _ FILES ['upload'] ['name' ] 未设置。以下是正在写入浏览器的回声:

lockquote

刚获得表单值

< >添加文本到电子邮件

目标=上传/

没有文件被发现的上传。

邮件建立...
$ b $发送了一封邮件!

这让我觉得我正在引用文件上传字段或上传本身不正确。



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

解决

  enctype =multipart / form-data

将您的表单更改为

 < form name =uploadmethod =postaction =send_form_email3.phpenctype =multipart / form-data> 


I'm using standard PHP functions to upload a file for use as an attachment with 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>

This form gets submitted to the following php handler where the mail is built and sent:

<?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!";
?>

The problem is that $_FILES['upload']['name'] is not set. Here's the echoes that are being written to the browser:

just got form values

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

This makes me think that I'm referencing the file upload field or the upload itself incorrectly.

Can anyone see any problems here or provide some guidance if this isn't the best way to do this?

解决方案

You have to add,

enctype= "multipart/form-data"

Change your form to,

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

这篇关于表单提交后$ _FILES为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆