Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有唯一文件名的联系人表单_Php_Forms_File - Fatal编程技术网

Php 具有唯一文件名的联系人表单

Php 具有唯一文件名的联系人表单,php,forms,file,Php,Forms,File,我想为我的联系人表单创建php文件,但每当他们提交时,我都需要一个不同的文件名.txt 这是代码。每当有人屈服时,我就会失去旧的 <?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST ['message']; $file = fopen('contactform.txt', "w+"); $content = $name. PHP_EOL .$email. PHP_EOL .$message;

我想为我的联系人表单创建php文件,但每当他们提交时,我都需要一个不同的文件名.txt

这是代码。每当有人屈服时,我就会失去旧的

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST ['message'];
$file = fopen('contactform.txt', "w+");
$content = $name. PHP_EOL  .$email. PHP_EOL  .$message;
header('Location: http://localhost:8080/site-edits/contact.php');
fwrite($file , $content);
fclose($file);
?>

您可以做的一件事是将当前日期添加到文件名中:

$file = fopen('contactform-'.date('YmdHis').'.txt', "w");
或者,您可以附加到文件,而不是在其上写入

$file = fopen('contactform.txt', "a");
$content = $name. PHP_EOL  .$email. PHP_EOL  .$message .PHP_EOL.PHP_EOL;

大多数人使用数据库(例如MySQL)来实现这一点。我建议您在有时间时学习如何使用数据库。

您可以在用户提交表单时使用不同的名称创建新文件,也可以使用
'a'
而不是
'w+'在现有文件中追加文本。
比如:


如需了解更多信息,请访问

,如巴里所说,您可以在姓名后附加一个时间戳

PHP还有一个可用于生成唯一文件名的

$file = fopen(tempnam(".", "contactform.txt"), "w+");

对我来说你的问题不清楚。。。为什么不使用数据库来保存数据?关于你的问题,如果你想为每次提交的表单创建一个file.txt文件,你必须用一个名字创建它。$file=fopen('contactform'.date('YmdHisu')。.txt','w+');或者,您可以将内容附加到其中file@Sfili_81这是一个项目,我的朋友这是为什么?这就是我被问到的,所以让我更清楚,当他们点击我的提交按钮时,我想让php创建一个类似contact_z(random).txt的文件,里面有email和他的名字message@Barry//我的天啊,我从没想过!谢谢我会在日期字符串的末尾加上“u”,以防同时提交两个表单,还应该有一个循环来确保名称不被接受我想你仍然会有一个竞争条件,你可以使用x(独占)fopen标志来解决这个问题,但是,老实说,如果您希望每秒写入多个数据,那么您应该使用数据库,而不是滚动自己的存储层。我会得到关于数据库的建议,但正如OP所说,要求是一个唯一的文件。。。我以前没有使用过x标志,这会使循环更有效,而不是单独检查文件是否存在,并声明“另一个文件”,而不是附加到同一个文件
$file = fopen(tempnam(".", "contactform.txt"), "w+");