使用fwrite()并在提交之前显示文本框中的文本,但每次我提交我的文本保存,然后在它之后保存一个数字


Using fwrite() and displaying the text in a text box before submitting but every time i submit my text saves and then a number is saved after it?

我使用fwrite()来写入。txt文件。

我有一个主页,其中显示来自.txt文件的文本。似乎工作正常。

然后我有一个'admin'页面,在提交时,将fwrite()我的文本区域的内容到文件…默认情况下,在页面加载时,文本区将显示.txt文件的当前内容,但每次我点击提交时,文本都被添加了,但是在它后面有一个数字值,它似乎是在计算字符,好像从0开始。

例如:

我输入'Hello'并提交/主页显示'Hello'/Textarea显示'Hello5'

…如果我再次点击提交按钮

首页显示'Hello5'/Textarea显示'Hello56'

等....

我似乎不明白为什么。

这是目前为止我所拥有的....

<?php
if(isset($_POST['submit1'])) {
$welcomeText=fopen("writetofile.txt", "w+");
$file_contents=$_POST['welcomeText'];
fwrite($welcomeText, $file_contents);
fclose($welcomeText);
}
?>

<form name="welcomeTextEditor" method="post" action="_admin.php">
    <textarea name="welcomeText" rows="4" cols="40"><?php echo(readfile("writetofile.txt")); ?></textarea>
    <br />
    <input type="submit" name="submit1" value="Save Changes" />
</form>

readfile返回从文件中读取并写入输出缓冲区的字节数,因此您不需要包含echo调用。

<?php readfile("writetofile.txt"); ?>