简体   繁体   English

文本文件未更新

[英]Text file isn't updating

I have this code: 我有这个代码:

<?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>

I have already made the text pages, and when I try to use this form the pages don't change. 我已经制作了文本页面,当我尝试使用此表单时,页面不会更改。 What am I doing wrong? 我究竟做错了什么? I'm incredibly new to html/php so I'm sure it's something stupid. 我是非常新的html / php,所以我确定这是一个愚蠢的东西。

For writing content in file you should use fwrite() after opening the file in write mode. 要在文件中写入内容,在写入模式下打开文件后应使用fwrite()

And in your code few mistakes are there, updated code is here. 在您的代码中,几乎没有错误,更新的代码就在这里。 I have supposed your file name is myfile.php - 我原以为你的文件名是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>

Also you can use the file_put_contents() function. 您也可以使用file_put_contents()函数。

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file. 此函数与连续调用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);
}
?>
<?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);
}
?>

And as Ritesh said, you aren't writing any data to your files. 正如Ritesh所说,你没有在文件中写入任何数据。 Just opening, and closing them. 打开,关闭它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM