PHP表单在上传大文件时会忘记值

时间:2015-08-11 06:05:58

标签: php forms

标题说明了一切......我有一个允许上传四个文件的联系表格。如果你上传大文件,表格实际上就会忘记"姓名,电话号码,电子邮件等。至少,我得到错误屏幕告诉用户提供该信息,我没有收到电子邮件。

如果我使用小文件(例如,一兆字节或更少),它工作正常,我得到文件,但更多的带宽,就像我提交一个空白表格,即使它经历了上传文件的动作(在我得到错误页面之前花了一分多钟)。

这里是表单本身的来源,包括一些PHP:

<?php
    ini_set("max_input_time","5");
    ini_set("max_execution_time","1");
    ini_set("upload_max_filesize","2048M");
    ini_set("post_max_size","2048M");
?>
<html>
    <head>
        <script language="Javascript">
            function formValidate() {
            // Did the user include a name?
            var userName=document.forms["contactRich"]["cName"].value;
            if (userName=="" || userName==null) {
                alert("Please provide your name.");
                return false;
            }

            // Did the user include a phone number and area code?
            var phoneNumber=document.forms["contactRich"]["cNumber"].value;
            var areaCode=document.forms["contactRich"]["cAreaCode"].value;
            if (phoneNumber=="" || phoneNumber==null || areaCode=="" || areaCode==null) {
                alert("Please include your phone number with the area code.");
                return false;
            }

            // Did the user provide a valid e-mail address?
            var emailAddress=document.forms["contactRich"]["cEmail"].value;
            var atCount=emailAddress.split("@").length-1;
            var lastAt=emailAddress.lastIndexOf("@");
            var isDot=emailAddress.lastIndexOf(".");
            var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test(emailAddress) || emailAddress=="" || emailAddress==null || emailAddress.length<5 || lastAt <0 || isDot<0 || isDot<lastAt || atCount!=1) {
                alert("Please provide a valid e-mail address.");
                return false;
            }

            // Did the user select a service?
            if (document.forms["contactRich"]["cTopic"].value=="null") {
                alert("Please tell us what you need help with.");
                return false;
            }

            document.getElementById("isValid").value="yes";
            return true;
        }
    </script> 
<!-- #BeginEditable "doctitle" --> 
<title>Contact Rich</title>
<!-- #EndEditable --> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.form {  font-family: "Franklin Gothic Medium", "Trebuchet MS"; font-size: 13pt}
-->
</style>
</head>

<body bgcolor="#CCCCCC">

<table width="800" border="0" cellspacing="2" cellpadding="2" align="center" bgcolor="#FFFFFF">
<form name="contactRich" id="contactRich" method="post" action="formHandler2.php" enctype="multipart/form-data" onsubmit="return fabFormValidate()">
    <table cellpadding="6">
        <tr>
            <td colspan="2"><h3>Contact</h3></td>
        </tr>
        <tr>
            <td class="form">Your name:</td>
            <td><input type="text" name="cName" size="31"></td>
        </tr>
        <tr>
            <td class="form">Your phone number:<br /><span class="footer">(Include area code)</span></td>
            <td>(<input type="text" size="3" maxlength="3" name="cAreaCode"/>) <input type="text" id="cNumber" name="cNumber" size="23"></td>
        </tr>
        <tr>
            <td class="form">Your e-mail address:</td>
            <td><input type="text" name="cEmail" size="31"></td>
        </tr>
        <tr>
            <td class="form">What can we help you with?</td>
            <td><select name="cTopic">
                    <option value="null">(Please choose:)</option>
                    <option value="an estimate">Estimate</option>
                    <option value="bifold doors">Bifold doors</option>
                    <option value="broken window ropes">Broken window ropes</option>
                    <option value="door that won't stay shut">My door won't stay shut!</option>
                    <option value="noisy doors">My door is noisy!</option>
                    <option value="sticking doors">My door is sticking!</option>
                    <option value="drywall repairs">Drywall repairs</option>
                    <option value="garbage disposals">Garbage disposals</option>
                    <option value="grab bars">Grab bars</option>
                    <option value="your various services">(other)</option>
                </select></td>
        </tr>
        <tr>
            <td class="form">Any additional details?</td>
            <td><textarea name="cAdditional" cols="27" rows="4" wrap="soft"></textarea></td>
        </tr>
        <tr>
            <td class="form">You may include up to<br />four pictures:</td>
            <td>
                <input type="file" name="file_1" id="file_1" /><br />
                <input type="file" name="file_2" id="file_2" /><br />
                <input type="file" name="file_3" id="file_3" /><br />
                <input type="file" name="file_4" id="file_4" /><br />
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Submit" /></td>
        </tr>
        <tr>
            <td colspan="2"><hr /></td>
        </tr>
    </table>
        <input type="hidden" id="isValid" name="isValid" value="no" />
</form>
</table>
<p>&nbsp;</p>
</body>
</html>

这是实际处理数据并尝试通过PHPMailer发送数据的PHP:

<?php
    ini_set("max_input_time","5");
    ini_set("max_execution_time","1");
    ini_set("upload_max_filesize","2048M");
    ini_set("post_max_size","2048M");
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <?php
            $error_exists   = false;
            $masterEmail    = "email@goes.here";
            require 'PHPMailer-master/PHPMailerAutoload.php';
            include "Form.php";
            $name           = $_POST['cName'];
            $areaCode       = $_POST['cAreaCode'];
            $phone          = $_POST['cNumber'];
            $email          = $_POST['cEmail'];
            $from           = "from_address@goes.here";
            $topic          = "(WEB) ".$_POST['cTopic'];
            $additional     = $_POST['cAdditional'];
            $isValid        = $_POST['isValid'];
            $headers        = "MIME-Version: 1.0" . "\r\n";
            $headers       .= "Content-type:text/html;charset=iso-8859-1" . "\r<br />&nbsp;<br />";
            $body           = "Name: ".$name."<br />&nbsp;<br />";
            $body           = $body."Phone number: (".$areaCode.") ".$phone."<br />&nbsp;<br />";
            $body           = $body."E-mail address: ".$email."<br />&nbsp;<br />";
            $body           = $body."Needs help with: ".$topic."<br />&nbsp;<br />";
            if (!empty($additional)) {
                $body = $body .$additional;
            }
            $body          .= "\n\nIP address: ".$_SERVER['REMOTE_ADDR']."<br />&nbsp;<br />";
            $body         .= "Browser: ".$_SERVER['HTTP_USER_AGENT']."<br />&nbsp;<br />";

            $mail = new PHPMailer();
            $mail->addAddress($masterEmail);
            $mail->setFrom($email, $from);
            $mail->Subject = $subject;
            $mail->msgHTML($body);
            if (isset($_FILES['file_1'])) {
                $mail->addAttachment($_FILES['file_1']['tmp_name'],$_FILES['file_1']['name']);
            }

            if (isset($_FILES['file_2'])) {
                $mail->addAttachment($_FILES['file_2']['tmp_name'],$_FILES['file_2']['name']);
            }

            if (isset($_FILES['file_3'])) {
                $mail->addAttachment($_FILES['file_3']['tmp_name'],$_FILES['file_3']['name']);
            }

            if (isset($_FILES['file_4'])) {
                $mail->addAttachment($_FILES['file_4']['tmp_name'],$_FILES['file_4']['name']);
            }

            if (empty($name)) {
                $errors = "Please provide your name.<br />";
                $error_exists = true;
            }

            if (!ctype_digit($areaCode) || strlen($areaCode) != 3 || strlen($phone) < 7) {
                $errors .= "Please provide your phone number, including area code.<br />";
                $error_exists = true;
            }

            if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $errors .= "Please enter a valid e-mail address.<br />";
                $error_exists = true;
            }

            if (empty($topic)) {
                $errors .= "Please tell us what you need help with.<br/>";
                $error_exists = true;
            }

        ?>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <style type="text/css">

        <title>Request for Information</title>
    </head>
    <body bgcolor="#cccccc">
        <?php
            $f=new fabForm();
            $f->setSubject($subject);
            $f->setBody($body);
            $f->setEmail($email);
            $f->setFrom($from);
        ?>
        <p>&nbsp;</p>
        <table align="center" bgcolor="#FFFFFF">
            <tr>
                <td>
                    <?php
                        if ($error_exists) {
                            echo $errors;
                            echo "&nbsp;<br />";
                            echo "<a href='contact.php'>Click here to try again.</a>";
                        } else {
                            if (!$mail->send()) {
                                echo "Mailer Error: " . $mail->ErrorInfo;
                            } else {
                                echo "Message sent!";
                        }
                    ?>
                </td>
            </tr>
        </table>
    </body>
</html>

最后,这是我使用的表单类:

<?php
class Form {
    private $email;
    private $subject;
    private $body;
    private $from;
    private $headers;

    // SET methods
    public function setEmail($emailAddress) {
        $this->email=$emailAddress;
    }

    public function setSubject($subjectLine) {
        $this->subject=$subjectLine;
    }

    public function setBody($bodyText) {
        $this->body=$bodyText;
    }

    public function setHeaders($sizeHeaders) {
        $this->headers=$sizeHeaders;
    }

    public function setFrom($whoFrom) {
        $this->from="From:".$whoFrom."\n".$this->headers;
    }

    // ACCESSOR METHODS
    public function getEmail() {
        return $this->email;
    }

    public function getSubject() {
        return $this->subject;
    }

    public function getBody() {
        return $this->body;
    }

    public function getHeaders() {
        return $this->headers;
    }

    public function getFrom() {
        return $this->from;
    }

    public function sendMail($sendHere) {
        if (mail(
                $sendHere,
                stripslashes($this->getSubject()),
                stripslashes($this->getBody()),
                $this->getFrom()
                )
           )
            echo("<p>Your request has been sent.</P>");
        else
            echo("<p>Due to technical difficulties, your request could not be delivered.</p>");
    }
}
?>

我可以发布PHPMailer源文件,但这几乎是一个完整的软件包...

但是什么会导致表格的数据真的被遗忘?我怎么能阻止它? (限制文件大小不是一个可行的选择。)如果有帮助,主机是1and1.com。

1 个答案:

答案 0 :(得分:0)

当文件大于post_max_size&#34; POST数据&#34;被PHP删除

解决方案是在提交表单之前通过AJAX发送文件。看这里知道怎么做:
Upload image with JavaScript from another server via AJAX