我看到了this question,我想在评论中提出一个问题,但没有足够的声誉。所以我在这里问这个问题。
我有一个HTML格式:
<form action="myprocessingscript.php" method="POST">
Name : <input name="field1" type="text" />
Email : <input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
php中的处理脚本,myprocessingscript.php
:
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$data = 'comments.txt' . $_POST['field1'] . ' ' . $_POST['field2'] . "\n";
$ret = file_put_contents('comments.txt', $data);
if ($ret === false) {
die('There was an error Sending The Comment');
}
else {
echo "The Comment Has Been Sent Succesfully !";
}
}
else {
die('Fill in The Form Please !');
}
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$data = 'comments.txt' . $_POST['field1'] . ' ' . $_POST['field2'] . "\n";
$ret = file_put_contents('comments.txt', $data);
if ($ret === false) {
die('There was an error Sending The Comment');
}
else {
echo "The Comment Has Been Sent Succesfully !";
}
}
else {
die('no post data to process');
}
当我将表单中的内容写入文本文件(comments.txt)时,之前的文本将被删除 - 我该怎么办?
答案 0 :(得分:5)
您只需要将“附加”标记添加到file_put_contents()
:
file_put_contents('comments.txt', $data, FILE_APPEND);
请参阅:http://uk3.php.net/manual/en/function.file-put-contents.php