php 记录擦除 txt

php records erasing txt

注册表单使用此代码将数据写入 txt 文件:

<?
if( isset( $_GET['list'] ) AND $_GET['list'] != '' ) {
$listId = $_GET['list'];
}
$email = $_POST['widget-subscribe-form-email'];
$fname = isset( $_POST['widget-subscribe-form-fname'] ) ? $_POST['widget-subscribe-form-fname'] : '';
$lname = isset( $_POST['widget-subscribe-form-lname'] ) ? $_POST['widget-subscribe-form-lname'] : '';


$fp = fopen("newsletter_subscriptions.txt","w+");

fputs($fp, "email : ");
fputs($fp, $_POST['widget-subscribe-form-email']);

fputs($fp, "\nPrénom : ");
fputs($fp, $_POST['widget-subscribe-form-fname']);

fputs($fp, "\nNom : ");
fputs($fp, $_POST['widget-subscribe-form-lname']);

fclose($fp);

?> 

我的问题是每条新记录都会抹掉之前的记录。我想将所有记录保存在 txt 文件中。 怎么做?

File open modes:

w+ Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

a+ Open for reading and writing; place the file pointer at the end of the file.

您打开文件的方式有误。 From the manual:

'w+' 读写打开;将文件指针放在文件的开头并将文件截断为零长度。如果该文件不存在,请尝试创建它。

你想附加到文件,你应该使用:

'a+' 读写打开;将文件指针放在文件末尾。如果该文件不存在,请尝试创建它。在此模式下,fseek() 仅影响读取位置,写入始终附加。

fopen 行更改为:

$fp = fopen("newsletter_subscriptions.txt","a+");