如何为每次提交将表单数据保存为不同的文件名?

时间:2011-05-05 00:35:20

标签: php forms

我有一个通过电子邮件发送数据并将其保存到文件中的表单,但我希望每次都将其保存到不同的文件中。

这是我的代码:

<?php
    // Contact Form

    // get posted data into local variables
    $EmailFrom = "gmail.com";
    $EmailTo = "mail@gmail.com";
    $Subject = "$Website";
    $Website = Trim(stripslashes($_POST['Website'])); 
    $Title = Trim(stripslashes($_POST['Title'])); 
    $Keywords = Trim(stripslashes($_POST['Keywords'])); 

    // validation
    $validationOK=true;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Website: ";
    $Body .= $Website;
    $Body .= "\n";
    $Body .= "Title: ";
    $Body .= $Title;
    $Body .= "\n";
    $Body .= "Keywords: ";
    $Body .= $Keywords;
    $Body .= "\n";

    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
        $string = '"Website","Title","Keywords"' . PHP_EOL;
        $string .= "\"$Website\",\"$Title\",\"$Keywords\"" . PHP_EOL;
        file_put_contents('formdata.txt', $string); // write file
        print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
    }

    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }
?>

1 个答案:

答案 0 :(得分:1)

这是一个使用当前时间的快速解决方案:

file_put_contents('formdata_' . time() . '.txt', $string); // write file