0

我有这个代码:

<html>
<head>
<title>Chipperyman573</title>
<link rel="shortcut icon" href="/fav.ico" />
</head>
<body>
<form method="post" action="change.php">
  Top: <input type="text" name="top" /><br>
  Bottom: <input type="text" name="bottom" /><br>
  Time (MS): <input type="text" name="time" /><br>
  <input type="submit" value="Save" name="submit" />
</form>
</body>
</html>

<?php
if(isset($_POST['submit'])){
$fi = "/var/www/rtf/webR/top.txt";
file_put_contents($fi, $cont);
$cont = $_POST["top"];
}
?>

当我转到更改页面 (chipperyman573.com/rtf/webR/change.php) 并填写表格,然后单击提交时,文本文件会自行清除。为什么?

我想清除文本文件的当前内容并将它们替换为我输入的内容。

4

3 回答 3

1

因为您在设置$cont. 它应该是:

$cont = $_POST["top"];
file_put_contents($fi, $cont);

或者简单地说:

file_put_contents($fi, $_POST["top"]);
于 2013-10-24T00:13:05.387 回答
1

$cont需要先定义, before file_get_contents()

于 2013-10-24T00:13:30.610 回答
1

因为您在 file_put_contents 之后声明了 $cont:

if(isset($_POST['submit'])){
$fi = "/var/www/rtf/webR/top.txt";
file_put_contents($fi, $cont);
$cont = $_POST["top"];

应该:

if(isset($_POST['submit'])){
$fi = "/var/www/rtf/webR/top.txt";
$cont = $_POST["top"];
file_put_contents($fi, $cont);
于 2013-10-24T00:13:33.383 回答