doumaqing6652 2011-09-05 18:43
浏览 18
已采纳

如何使用PHP创建表单来更新文件?

I have written a PHP script which allows me to modify and update a file on a webpage, which works, but the script right now presents the entire contents of the file in a text area. I would like to extract just the string from the file and pre-populate it in a form text field (instead of a text area).

Below are the two files I need to update. I would like to just update the IP addresses. So the form would ideally show that mail.helloworld.com current maps to "208.164.222.2" and populate that IP address in a form text field. Same idea for the /etc/postfix/main.cf file as shown below.

/etc/hosts

IPAddress Hostname Alias 127.0.0.1 localhost deep.openna.com 208.164.222.2 mail.helloworld.com mail 208.164.222.3 web.helloworld.com web

/etc/postfix/main.cf

relayhost= 192.168.1.10

===

Current working script using textarea:

<form action="<?php echo $PHP_SELF;?>" method="post">
<textarea rows="13" cols="110" name="content">
<?
$fn = "/etc/postfix/main.cf";
print htmlspecialchars(implode("",file($fn)));

?> 
</textarea></br>
<input id= "relayhost_button" type="submit" value="Update"> 
</form> 



<?
$fn = "/etc/postfix/main.cf";
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=done.php\" />
";
?>

Here is the current textarea based form:

enter image description here

New text field based form I want:

enter image description here As always, thanks folks!

  • 写回答

1条回答 默认 最新

  • duan0818 2011-09-05 18:58
    关注

    Parse the file, then present the wanted line to the user. Once (s)he has edited it, reparse the file, and replace the information. (Instead of parsing, you could just track which line number it was.)

    Other than that, the question gets a bit in depth. Do you have any specific problems?

    Also, a few non-related suggestions:

    • Replace the implode(file()) with file_get_contents.
    • Use complete opening tags ('< ?php' without the space)
    • Use fwrite instead of fputs (no actual justification for this one, I just avoid using aliases)
    • A large part personal preference, but I find it cleaner to do redirects with headers than with meta tags.

    Eg:

    header("Location: http://blah/done.php");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch