繁体   English   中英

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

[英]How can I save form data to a different filename for each submission?

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

这是我的代码:

<?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\">";
    }
?>

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM