空空如也,什么都不做

Form empty itself without doing anything

今天早些时候,我问了一个关于我做错了什么的问题,我可以正常工作,但现在我遇到了这个脚本的另一个问题。

之前

代码给出了警告(),现已修复。您可以阅读 post Here

有什么问题?

代码自动清空。当您刷新脚本所在的页面时,文本文件为空。我不知道为什么... 这是代码

    <?php 
$fn = "file.txt"; 
$file = fopen($fn, "w+"); 
$size = filesize($fn); 

if($_POST['addition']) fwrite($file, $_POST['addition']); 

fclose($file); 
?> 
<form action="<?=$PHP_SELF?>" method="post"> 
<input type="text" name="addition" value="<?php echo file_get_contents('file.txt');?>"/> 
<input type="submit"/> 
</form>

我使用这个脚本在我的网站上显示一个 youtube 视频,所以我必须经常更新它。 您可以通过此链接找到脚本的工作示例:http://beta.martijnmelchers.nl/private/Test/test.php

我尝试了什么?

我没有尝试太多,因为我在互联网上找不到解决方案,在代码中也找不到。 请再次帮助我!提前致谢!

根据 manualw+ 选项:

Open for reading and writing; 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.

看起来您想在创建 post 时替换所有内容,因此最简单的解决方案是将所有文件处理调用置于 POST 条件中:

// To avoid warnings, this is better.
// You can add your original condition after it if you need it.
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
  $file = fopen($fn, "w+"); 
  // not sure why you need this...
  // $size = filesize($fn);
  fwrite($file, $_POST['addition']);
  fclose($file);
}