我有一个php脚本用于用户输入,现在我希望这个脚本"添加到现有文件data.txt(首选("或为每个答案制作一个单独的文件,名为$field1
<?php
$txt = "data.txt";
$fh = fopen($txt, 'w+');
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$txt=$_POST['field1'].' - '.$_POST['field2'];
file_put_contents('data.txt',$txt."n",FILE_APPEND); //log to data.txt
exit();
}
fwrite($fh,$txt); // write information to the file
fclose($fh); // close the file
?>
这是网站上的一个表格,必须写
"Name - Vote"
"Name - Vote"
"Name - Vote"
现在它覆盖文件,而不是添加
@Eric:阅读插入到解析代码中的注释
<?php
$txt = "data.txt";
$fh = fopen($txt, 'a'); // the correct open flag for append end of file
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh, $txt . "n"); // carriage return added (*assumption needed)
// exit(); // omit
}
// fwrite($fh,$txt); // write information to the file // pointless, redundant
fclose($fh); // close the file
?>