文本文件未更新

时间:2013-10-18 04:12:59

标签: php

我有这段代码:

<?php
if(isset($_POST['submit'])){
$fi = "/home2/chippery/public_html/rtf/webR/top.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["top"];
fwrite($fih,$cont);
fclose($fih);


$fi = "/home2/chippery/public_html/rtf/webR/bottom.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["bottom"];
fwrite($fih,$cont);
fclose($fih);


$fi = "/home2/chippery/public_html/rtf/webR/nextTime.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["time"];
fwrite($fih,$cont);
fclose($fih);
}
?>

<html>
<head>
<title>Chipperyman573</title>
<link rel="shortcut icon" href="/fav.ico" />
</head>
<body>
<form method="post" action="change.html">
  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" />
</form>
</body>
</html>

我已经制作了文本页面,当我尝试使用此表单时,页面不会更改。我究竟做错了什么?我是令人难以置信的新的html / php,所以我确定这是愚蠢的。

3 个答案:

答案 0 :(得分:3)

要在文件中编写内容,在写入模式下打开文件后应使用fwrite()

在您的代码中,几乎没有错误,更新的代码就在这里。我认为你的文件名是myfile.php -

<?php
if(isset($_POST['submit'])){
$fi = "top.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["top"];
fwrite($fih,$cont);
fclose($fih);


$fi = "bottom.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["bottom"];
fwrite($fih,$cont);
fclose($fih);


$fi = "nextTime.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["time"];
fwrite($fih,$cont);
fclose($fih);
}
?>
<html>
<head>
<title>Chipperyman573</title>
<link rel="shortcut icon" href="/fav.ico" />
</head>
<body>
<form method="post" action="myfile.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" />
</form>
</body>
</html>

答案 1 :(得分:1)

您还可以使用 file_put_contents()功能。

  

此函数与调用fopen(),fwrite()和fclose()相同   先后将数据写入文件。

<?php
if (isset($_POST['submit'])) {
    $fi = "top.txt";
    $cont = $_POST["top"];
    file_put_contents($fi, $cont);


    $fi = "bottom.txt";
    $cont = $_POST["bottom"];
    file_put_contents($fi, $cont);


    $fi = "nextTime.txt";
    $cont = $_POST["time"];
    file_put_contents($fi, $cont);
}
?>

答案 2 :(得分:0)

<?php
if(isset($_POST['submit'])){
$fi = "top.txt";
$fih = fopen($fi, 'w') <-- missing semi-colon
$cont = $_POST["top"] <-- missing semi-colon
fclose($fih);


$fi = "bottom.txt";
$fih = fopen($fi, 'w') <-- missing semi-colon
$cont = $_POST["bottom"];
fclose($fih);


$fi = "nextTime.txt";
$fih = fopen($fi, 'w') <-- missing semi-colon
$cont = $_POST["time"];
fclose($fih);
}
?>

正如Ritesh所说,你不是要在文件中写入任何数据。打开并关闭它们。